PDA

View Full Version : PHP Redirect


inthezone
02-27-2003, 01:44 AM
I'm writing a conditional script that uses vB's global.php, and I'm attempting to redirect unregistered users to the user cp login screen. However, since I am using a header include on the page, I get a Cannot modify header information - headers already sent error. I've tried commenting out the line that requires the header, and the redirect works fine. With the require line intact, I get the error and no redirect.


<?php

chdir('/path/to/forums/');
require('./global.php');

require('/path/to/header.php');

if ($bbuserinfo['usergroupid']==1)
{
header("Location: http://www.mydomain.com/forums/usercp.php");
exit;
}
else
{
echo "Welcome back, ".$bbuserinfo['username']."!";
}

require('/path/to/footer.php');

?>

How can I get it to redirect properly while still using the header? (BTW, header.php and footer.php are strictly HTML with no php code inside.)



Edit-I guess putting the header and footer requires in the "else" part of the conditional would work. Is there another way to do this that I am missing?

mr e
02-27-2003, 02:09 AM
use a javascript redirect, not sure what it is exactly but it works, or you could try a meta redirect but im not sure if that'll work

Kriek
03-01-2003, 10:09 PM
Output Buffering

Use output buffering with ob_start() (http://www.php.net/manual/en/function.ob-start.php) and ob_end_flush() (http://www.php.net/manual/en/function.ob-end-flush.php). Although ob_end_flush() (http://www.php.net/manual/en/function.ob-end-flush.php) isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start() (http://www.php.net/manual/en/function.ob-start.php)

<?php ob_start();

// entire script, code, or page here

ob_end_flush(); ?>

Output buffering (PHP 4.0) was originally designed to solve HTTP header errors. I should also mention that Output buffers are stackable, that is, you may call ob_start() (http://www.php.net/manual/en/function.ob-start.php) while another ob_start() (http://www.php.net/manual/en/function.ob-start.php) is active. Just make sure that you call ob_end_flush() (http://www.php.net/manual/en/function.ob-end-flush.php) the appropriate number of times. If multiple output callback functions are active, output is being filtered sequentially through each of them in nesting order.

VeoMorphine
03-03-2003, 06:38 PM
check your global.php for any spaces or new lines after the ?> and before the <?