Header("Location") on PHP

So check out this code…..

if(1== 1) { header("Location: index.php"); }
header("Location: http://www.google.com");

This simple code will be expected to redirect to index.php since 1==1 is always true. And for a weird reason it returns to redirect to www.google.com.

Maybe for a reason i didn’t quite understand this, but it should be written like this

if(1== 1) {
	header("Location: index.php");
}
else {
	header("Location: http://www.google.com");
}

As mentioned before the only logical explanation is that this command is a client side command that means the script doesn’t stop from being executed till end. My guess is that header location, first of all has to be executed before anything has sent to html output, so it does fill the html with this line
.

6 replies on “Header("Location") on PHP”

Any ideas why the following line won’t work:

header(‘Location: index.php? page=list-album’);

but the following line does work as a test to see if Header works:

header(‘location: index.html’)

Dont Forget that header(“Location: somewhere.html”); can only work when nothing else has been output to page.
Place it above all.

Error Suppression Can Hide the error message
warning: Cannot modify header information – headers already sent by ….

Leave a Reply to worthposting Cancel reply

Your email address will not be published.