June 27, 2013

PHP: header alternative?

Ralph’s Question:

I wanted to know if the echoing the following would be a good alternative to using header(“Location: index.html”) without having to use output_buffering

echo "<script type='text/javascript>window.location = 'index.html';</script>";

There are times I would like to redirect possibly in a body, would the above work fine or is it worth turning on the output_buffering option and just user header()?

header() defines the page the user is viewing. Using output buffering forces to ignore the previous output buffer, so it is not considered an efficient way to redirect. So, here are my suggestions:

  1. If your only target is to redirect, then make sure you output nothing before header statement executes. In this case header is appropriate.

  2. If you need to show your page and based on user interactivity need to redirect to the different page, use the JavaScript Redirection.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!