PHP Header Location Redirect Not Working

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;
?>

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 redirect not working

Look carefully at your includes - perhaps you have a blank line after a closing ?> ?

This will cause some literal whitespace to be sent as output, preventing you from making subsequent header calls.

Note that it is legal to leave the close ?> off the include file, which is a useful idiom for avoiding this problem.

(EDIT: looking at your header, you need to avoid doing any HTML output if you want to output headers, or use output buffering to capture it).

Finally, as the PHP manual page for header points out, you should really use full URLs to redirect:

Note: HTTP/1.1 requires an absolute
URI as argument to Location:
including the scheme, hostname and
absolute path, but some clients accept
relative URIs. You can usually use
$_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to
make an absolute URI from a relative
one yourself:

PHP Redirect to different url using header(Location:) does not work

Headers must be set before any data is transmitted, so you can't just stick them in the middle of a file. Quoting the the manual:

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

So at the very least you'll need to rewrite your file to:

<?php

header("Window-target: _parent");
header("Location: https://www.w3schools.com/");

?>
<!doctype html>
...

Also, never sleep() in an http(s) response: that response should finish as fast as it can, no matter what content it needs to generate. Sleep has no place in (really any) PHP code.

PHP Header location redirect not working. How to rectify

It's due to the spaces at the beginning of the file.

Make sure there is nothing out of <?php and ?> before the redirect is done as the headers should be sent before any content of the page has been sent. If you enable the php warnings there should be a warning about this. Also make sure there is nothing printed in the included connection.php file before the redirect is done.

In your case some spaces have been sent so that the header redirection will not be done.

It could be still done if the web server or the script had an output buffer but since you say it's not working it doesn't seem to be the case.

The PHP header() function is not redirecting

If you're using header("Location: "); after you've output content make sure you've put ob_start(); earlier in the script. ob_start(); buffers your script, so nothing is output until you either call ob_end(); or the end of the file is reached. Calling header("Location: "); after content has already been output to the browser can cause problems.

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.

header('Location:') not redirecting

You need to make sure you don't output anything before the header tag. Since it was working before, I assume it's not an error where you are echo'n stuff before the call to header, and since you see no output, I assume it is outputting white-space. There are a few places where some whitespace and/or invisible characters can sneak into a file:

Put the cursor before your opening PHP tag (<? or <?php) and hit backspace a few times, then hit delete to delete the < and then retype the <. That will make sure there are no invisible characters being output to the browser before your headers.

Then go through any files that are included by that file, and do the same thing, as well as delete any ?> that you have at the end of the file. If those files include any othe files repeat this on them.

PHP header redirect not working

Try:

error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);

flush();
header("Location: http://www.website.com/");
die('should have redirected by now');

See what you get. You shouldn't use ^ (xor) in your error_reporting() call because you're unintentionally asking for all errors EXCEPT notices and warnings.. which is what a 'headers already sent' error is.

Edit:

Also try putting flush() right above your header() call.



Related Topics



Leave a reply



Submit