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 on 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, 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 editor.

Find this part of the file:

; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = Off

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.

Restart MAMP.

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.

Sorted!


This post currently has 23 responses

  1. Austin says:

    I followed the instruction, where your changing the php.ini file. I am changing the php.ini file, but it doesn’t update on my info.php, and its my headers are still not working. thanks!

  2. Jake says:

    I am having the same problem. I changed them in php5.4.4 which is the version I am using according to info.php and still nothing has changed.

  3. Arvin says:

    Thanks for the tip. 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.

  4. Márcio Serrado says:

    You saved my day… but please update the post:

    We must edit 2 files as Arvin noted:

    Thanks for the tip. 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.

  5. guyda says:

    That other php.ini solution saved my life. thanks a lot for finding that and sharing it with us.

  6. jeffrey says:

    Thanks for posting this ace solution. Saved me, and I’m sure many others who haven’t posted, hours of frustration and sanity.

    Cheers!

  7. nunzio says:

    Ohhhh yeeeppp

    thanks!!

  8. nabira says:

    ok thanks a lot,,,,, it’s work!

  9. mostafa says:

    Thanks a lot guys <3

  10. sathish says:

    Thanks a lot.. Really work.. i solved

  11. Julia says:

    Thank you so much, I had a site working with XAMPP and not with MAMP and after hours of googling, this finally helped me. I am truly grateful!

    • hibbard.eu says:

      Hey Julia,

      Thanks for taking the time to comment. I’m glad the post proved useful.

  12. Nartub says:

    Just for the record, if you have MAMP Pro, you need to change a third file located in :
    1) Go to App/MampPro (right click, Show Package Content)
    2) Go to Contents/Resources/
    3) You’ll find lots of php.x.xx.ini files. Change the version you use.
    4) Restart Mamp and that will do the trick.

    ps. (I changed the other files in the MAMP folder, so I’m not sure if only changing this file or the three of them does the trick)
    Good luck!

  13. Roberto says:

    Thanks!! You saved me!!!

  14. nenu says:

    Thanks this saved a me a lot of time!

  15. JOB SUNDAY says:

    header is not working and i have imported the files to the internet. How can i change the settings as you had suggested. Thanks

Comments are closed!