Php header location redirect not working - Stack Overflow
PHP Header Location not working - PHP - SitePoint Forums | Web Development & Design Community
Why is header("Location: contact.php?status=thanks"); redirect not working?
Header redirect not working - PHP - SitePoint Forums | Web Development & Design Community
Videos
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;
?>
Try adding ob_start(); at the top of the code i.e. before the include statement.
This is likely a problem generated by the headers being already sent.
Why
This occurs if you have echoed anything before deciding to redirect. If so, then the initial (default) headers have been sent and the new headers cannot replace something that's already in the output buffer getting ready to be sent to the browser.
Sometimes it's not even necessary to have echoed something yourself:
- if an error is being outputted to the browser it's also considered content so the headers must be sent before the error information;
- if one of your files is encoded in one format (let's say ISO-8859-1) and another is encoded in another (let's say UTF-8 with BOM) the incompatibility between the two encodings may result in a few characters being outputted;
Let's check
To test if this is the case you have to enable error reporting: error_reporting(E_ALL); and set the errors to be displayed ini_set('display_errors', TRUE); after which you will likely see a warning referring to the headers being already sent.
Let's fix
Fixing this kinds of errors:
- writing your redirect logic somewhere in the code before anything is outputted;
- using output buffers to trap any outgoing info and only release it at some point when you know all redirect attempts have been run;
- Using a proper MVC framework they already solve it;
More
MVC solves it both functionally by ensuring that the logic is in the controller and the controller triggers the display/rendering of a view only at the end of the controllers. This means you can decide to do a redirect somewhere within the action but not withing the view.
I have experienced that kind of issue before and now I'm not using header('Location: pageExample.php'); anymore, instead I'm using javascript's document.location.
Change your:
header('Location: page1.php');
To something like this:
echo "<script type='text/javascript'> document.location = 'page1.php'; </script>";
And what is the purpose of echo $_POST['cancel']; by the way?, just delete that line if what you want is just the redirection. I've been using that <script> every time and it doesn't fail me. :-)
I wanna make a redirection.
I tried to do it by using
<?php header('Location: /member.php'); ?>
and
<?php header('Location: member.php'); ?>
But nothing happens, the redirection doesn't work.
I tried to add only this code in a blank page and it's not working either.
Is it possible that the hosting company blocked this ?
Can I activate this in a .htaccess file ?
thx
Try changing:
header("Location : blabla")
^
|
(whitespace)
To
header("Location: blabla")
Well, if the server sends a correct redirection header, the browser redirects and therefore "changes the url". It might be a browser issue, then. I don't know if it has anything to do with it, but you should not send a relative url in the location header ("HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. ", http://php.net/manual/en/function.header.php), and "location" must be capitalized, like:
header('Location: http://myhost.com/mypage.php');
Looks like you're echo-ing text to the browser before sending the header('location'). You can not send content to your browser before executing a header(), as your echo will force a header to be sent. Comment these lines out and see if it works:
// echo $path_choice;
// echo $page_inc;
Now your header will be sent and you will be redirected.
add this code to first of code:
<?php
ob_start();
?>