Once you output any content (an echo statement, or white space or text outside of PHP), then you can no longer modify the header information. Someone posted a similar problem recently, with a link to a good explanation on Stack Overflow: https://teamtreehouse.com/forum/redirecting-after-a-form-submission-not-functioning Answer from Randy Hoyt on teamtreehouse.com
🌐
PHP
php.net › manual › en › function.header.php
PHP: header - Manual
If you want to redirect an user and tell him he will be redirected, e. g. "You will be redirected in about 5 secs. If not, click here." you cannot use header( 'Location: ...' ) as you can't sent any output before the headers are sent.
Discussions

Php header location redirect not working - Stack Overflow
How can I fix this? page1.php is a real page located in the same folder as the current page. The above code is the very first lines of the php file. Nothing before it. Nothing. Not even whitespace. ... Don't echo anything afterwards; do an exit() to terminate the script. (You shouldn't output anything after having issued that header... More on stackoverflow.com
🌐 stackoverflow.com
PHP Header Location not working - PHP - SitePoint Forums | Web Development & Design Community
I am treading lightly because of my lack of experience with PHP and MySql. Only got notice of 1 reply. Glad to see others. Thanks again. ... If you use the Live HTTP Headers add on for Firefox, and log in to the site, do you see the "Location: " header in the response at all, or doesn’t it ... More on sitepoint.com
🌐 sitepoint.com
0
October 26, 2011
Why is header("Location: contact.php?status=thanks"); redirect not working?
Joan Ang is having issues with: The "contact.php" page loads fine, but when I submit the form, the address remains "contact.php", and doesn't load (this is because of exi... More on teamtreehouse.com
🌐 teamtreehouse.com
2
July 24, 2014
Header redirect not working - PHP - SitePoint Forums | Web Development & Design Community
Newbie coder here with another odd problem. I have a redirect that only works sometimes, but most frequently it does not. I do have this code placed prior to my html tag. I know it is the above code that is the issue, as when I put in a testing code (echo ‘testing’) it does show that, but ... More on sitepoint.com
🌐 sitepoint.com
0
June 24, 2020
Top answer
1 of 14
26

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.

2 of 14
25

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. :-)

🌐
Edureka
edureka.co › blog › header-location-in-php
Header Location In PHP | PHP Header Location Edureka
February 25, 2025 - It must be called before sending any actual output, either by normal HTML tags, blank lines in a file or from a PHP file. Syntax: header(string,replace,http_response_code); string: It consists of a header string. Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory.
🌐
SitePoint
sitepoint.com › php
PHP Header Location not working - PHP - SitePoint Forums | Web Development & Design Community
October 26, 2011 - When you look at the page, press CTRL + SHIFT + L , and a bar will appear at the left side of the screen · Alternatively, you could go to View (ALT + V) > Sidebar > Live HTTP Headers · When it works you will get a bar at the left side on the ...
🌐
Team Treehouse
teamtreehouse.com › community › why-is-headerlocation-contactphpstatusthanks-redirect-not-working
Why is header("Location: contact.php?status=thanks"); redirect not working? (Example) | Treehouse Community
July 24, 2014 - If I change <form method="post" action="contact.php"> to <form method="post" action="contact-process.php"> it works fine. Stackoverflow says it has something to do with unintentional white space but I still haven't figured out how to fix it. header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP http://stackoverflow.com/questions/12525251/header-location-not-working-in-my-php-code ·
Find elsewhere
🌐
Bobcares
bobcares.com › blog › php-header-not-redirecting
PHP header not redirecting- How we fix it?
February 9, 2021 - It is important to make the ... errors?- We’ll help you.] In short, the PHP header not redirecting error occurs mainly due to the absence of ob_start() function, incorrect header formats, and so on....
🌐
SitePoint
sitepoint.com › php
Header redirect not working - PHP - SitePoint Forums | Web Development & Design Community
June 24, 2020 - I have read some posts on others having similar problems, and the recommendation is to use a Javascript redirect instead. Is this a better way of resolving this? (It sounds like it is). However I notice that there are 2 methods described: <script type='text/javascript'> document.location = 'page1.php'; </script>
🌐
W3Docs
w3docs.com › php
PHP header location-redirect doesn't work - why?
Headers have already been sent: If headers have already been sent by the server, the header function will not work. This can happen if there is a space or a new line after the closing PHP tag in a file that is included by the current script.
🌐
Hibbard
hibbard.eu › php-headerlocation-not-working-in-mamp
PHP – header('Location: ...') not working in MAMP · James Hibbard
January 11, 2013 - For those having redirect problems after editing the php.ini file inside the MAMP/conf/(your php version) folder, there’s another php.ini file located at MAMP/bin/php/, I also edited that one and the header(Location… problem was solved. ... That other php.ini solution saved my life. thanks a lot for finding that and sharing it with us. ... Thanks for posting this ace solution. Saved me, and I’m sure many others who haven’t posted, hours of frustration and sanity. ... You’re welcome, Jeffrey. Thanks for taking the time to let me know that this worked for you.
🌐
x10Hosting
community.x10hosting.com › forums › site design, development, and promotion › scripts, 3rd party apps, and programming
PHP header location not working on X10hosting | x10Hosting: Free Hosting Community
December 9, 2011 - You may have a problem with a Byte Order Mark in your file -- an invisible, unprintable character that will appear as the first two bytes of a Unicode text file to tell the system whether the file uses Big-endian or Little-endian byte orders. If you are suppressing errors, you will not be told about the "headers already sent" error, but any custom headers you try to send will not be sent. ... Paste your script. It's a bug in the script - header location works fine for me.
🌐
SitePoint
sitepoint.com › php
I have a problem with header() - PHP - SitePoint Forums | Web Development & Design Community
November 6, 2023 - In my website, all pages which are visible to the user include 3 major files: the top part in page_header.php The bottom is page_footer.php The middle is the page content. In thr error_log file I constantly get the warning: PHP Warning: session_start(): Session cannot be started after headers have already been sent in /home4/traderan/public_html/pages/page_header.php on line 2 Here is the content of page_header.php ( the upper part):
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 291972 › header-location
php - header location [SOLVED] | DaniWeb
maybe because in the first place it didn't satisfy your if statement ... because if it does, it should redirect... i don't see anything wrong on the php header() function — vaultdweller123 32 Jump to Post · or you could try this, if u think the header() still doesn't work · echo "<script>window.location='http://www.websitename/members/securecode/index.php'</script>"; — vaultdweller123 32 Jump to Post
🌐
InfinityFree
forum.infinityfree.com › hosting support
Using header in php - Hosting Support - InfinityFree Forum
February 9, 2021 - What exactly were you putting as the header? I know from experience that it works perfectly fine · Well I was making a login page using files and the code looked something like this if (usernameexists() === true) { header(“Location: usernameused.html”); } · The forum supports Markdown, ...
🌐
Roots Discourse
discourse.roots.io › t › roots-7-0-1-php-header-location-not-working › 2206
Roots 7.0.1 - PHP Header Location Not Working - Roots Discourse
August 31, 2014 - I've done the 7.0.1 update Roots nguongame.com project. And all PHP Header Location not working. For Roots 6.5.2 is fully working. An example: I redirect all 404 on the homepage but it does not work. My code: <…
🌐
Team Treehouse
teamtreehouse.com › community › header-redirect-not-working-on-localhost
header redirect not working on localhost. (Example) | Treehouse Community
December 19, 2014 - You might need to check if there is a blank space before <?php include("inc/products.php"); I had the same problem, but mine had one tiny blank space before that code. I removed it then the file started working. It is worth checking it if it is still not working!