PHP header() function not working: php header() not redirecting


Posted on 07 August 2016

While building a website or a web application a developer faces many problems and today i am going to share one of the most common problem which every php programmer faces while using the header() function, i faced this problem too in my early days of coding,

php header() not working

Also read:How-to-Create-RSS-feeds-using-PHP-and-MySQL

Let us first see what php header() function actually do, the header() function is used to redirect to a given url. The syntax for the header()  function is shown below:


Now let us see a simple redirection script using php


The above script will redirect you to, now we will see what the problem actually is while using the header() function. When we are coding large applications sometimes we do the outputs before redirection due to which the output does not get stored in the buffer and the header function does not catch the URL due to which the redirection does not work.

So to solve this problem we need to store the output in a buffer and need to flush that buffer at the end of the code.

So we use a php function called ob_start() to buffer all the output of our php code and ob_end_flush() to flush all the output when the execution of the whole code is done.

So whenever you are facing such problem like your header() function is not redirecting then just follow this simple rule to solve it. Below is the code to show how to use ob_start()  and ob_end_flush().


//more code


Just put the ob_start() function in the starting of your code and ob_end_flush() in the end. This will definetly solve your problem.

Thank you for reading.

Tweet your queries and feedback to @PsychoCodes or leave a message on our Facebook . You can also comment your questions below.

Also, don't forget to subscribe to our Newsletter at top of this page.

If you like this article, then please share it and help us grow.

If You Love this article, You Should Consider:

  • Like us on Facebook
  • Follow us on Instagram
  • Follow us on Twitter
  • Subscribe to our Newsletter.
  • Let us know your suggestions and queries in the comments below.

Thank you for your Love and Support

Share your thoughts