Header Location Not Working in My PHP Code

header location not working in my php code

That is because you have an output:

?>
<?php

results in blank line output.

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

Combine all your PHP codes and make sure you don't have any spaces at the beginning of the file.

also after header('location: index.php'); add exit(); if you have any other scripts bellow.

Also move your redirect header after the last if.

If there is content, then you can also redirect by injecting javascript:

<?php
echo "<script>window.location.href='target.php';</script>";
exit;
?>

header('Location:') not working

1) Answer to your 1st Question (When using header('location: newHome.php') Does that file need to be in the same directory?)

If you will not give any path then Yes it must be in the same directory.

Though, you need to define constant for your website URL like below:

define(SITE_URL,"http://www.example.com/");

And then use it anywhere in your website like below:

<li><a href="<?php echo SITE_URL; ?>signOut.php"><strong>Sign Out</strong>

Follow below link to learn more about Constants :

http://php.net/manual/en/function.constant.php

2) Answer to your 2nd Question (My code just returns a error page at example.com/signOut.php)

This is happening because your path is not correct. Manually correct the path or follow my first answer and define constant to correct it.

I don't think there is any issue with Headers as error you are saying with 404 Not found.

Php header location redirect not working

Pekka answered my question in the comments. He didn't post an answer, so I am now. Use the exit() method after the header redirect. For some reason the rest of the code of the page continues to execute after the header() method redirect. When the rest of the code executes, the echo statement is outputted to the page. And you can't redirect using the header function after you output to the page. To avoid rest of the code from executing, use exit(). Thanks Pekka.

UPDATE: When using the web browser Internet Explorer, I have noticed that $_POST['cancel'] is not reliable. I am not exactly sure why this is, but I suspect IE posts additional variables on a form submit, specifically the variable 'cancel' is posted. I solved this by using a variable name other than 'cancel'. The combination of using exit() and a unique variable name is working for me.

PHP header() not working?

As Icewine pointed out correctly, headers should be set before any output is created.

So either move your code that is setting the header to the beginning of your script, or capture all output with output buffering, by calling ob_start() at the beginning of your script.

Also It is customary to not send any content when redirecting with a Location header, another reason to move your logic for the redirect to the beginning of your script, and then call exit() after setting the header.



Related Topics



Leave a reply



Submit