PHP – header(‘Location: …’) not working in MAMP
Recently, I was trying to help someone build a simple contact form in PHP.
This person was running everything locally in a MAMP server (Macintosh, Apache, Mysql and PHP) and everything was going swimmingly until we ran into a strange issue: PHP’s
header('Location: ...') wasn’t working as expected. In fact, to be fair, it wasn’t working at all.
This is how we solved the problem.
It turned out that output_buffering was disabled in his php.ini configuration file. All we had to do is turn it back on.
If this issue is affecting you, here are the steps you can take to correct it.
Check the current output_buffering value:
To do this, add the following code to a blank text file, then save it as info.php in the root folder of your MAMP installation (by default this is the MAMP htdocs folder which is located in the MAMP Application directory /Applications/MAMP).
<?php phpinfo (); ?>
Now navigate to http://localhost:8888/info.php and do an in-page search for the term “output_buffering”.
If the value displaying next to it is “no value” or something similar, then it’s turned off.
Turn output_buffering on:
Go to your MAMP folder -> conf -> php4 or/and php5 ->php.ini
Make a copy of this file in case something goes wrong.
Open php.ini in a text program
Find this part of the file:
The text might not be an exact match, but you can search for “output_buffering”
Change the last line to:
output_buffering = 4096
Save the file.
Go to http://localhost:8888/info.php again to check the value is 4096.
Test it’s working:
Here’s a mini script for you to copy and paste, just to check that everything is good.
Save this as redirect.php in your htdocs folder.
<?php header("Location: http://www.hibbard.eu"); ?>
Navigate to http://localhost:8888/redirect.php and you should be redirected back to my site.