(06-15-2014, 04:24 PM)Ex094 Wrote: (06-15-2014, 04:19 PM)dropzon3 Wrote: (06-15-2014, 04:07 PM)BlueCat Wrote: Do I place that anywhere in the php code? or?
It must be before you write any content.
ex.
Code:
echo "Hello user";
header("Location: http://google.com");
will fail because headers need to be written before content. You can use output buffering to prevent content from being written until after everything is processed but that isn't the default.
And yes that too! Good thing that he pointed out.. If you place header before a routine then the header function will redirect and render your statements below it useless, like in his example
Actually code after the header() call will still be run header("Location: ...") is not the same as die(); or exit(); the code continues to execute until you've left the page so code like this can be insecure:
Code:
if(!$_SESSION['is_admin']) header('Location: index.php');
//Run admin code now
$_SESSION['is_admin'] = true;
//Other admin code
The $_SESSION assignment will occur even though the header has been sent.
My main point was refering to the error:
Quote:Warning: Cannot modify header information - headers already sent by (output started at /var/www/demo/header.php:2) in /var/www/demo/header.php on line 3
The problem is that in an HTTP request and response the format is
HEADERS
[blank link]
page content
So if you write something that will be sent as page content you can't go back and add headers. So headers need to be sent before page content. Either by handling it before you try to echo or using output buffering.