Take the 2-minute tour ×

If I want to redirect a user in PHP, all I've ever known to do was use the header('Location:' http://www.example.com) but I've been reading that this isn't the best way to redirect a user from page to page internally. What are some other options you can redirect a user?

Example: at the bottom it says:

Something Important to Remember
...I don’t recommend, for example, using header() to bounce your users around to different pages; there are better methods that reduce the number of page loads and give the user a more fluid experience...


share|improve this question
you can also redirect from client side: window.url = 'newUrl'; –  ThatGuy Aug 1 '11 at 15:50
add comment

5 Answers

up vote 1 down vote accepted

The snippet you provided is referring to issues where page1.php might execute some code followed by header('Lodation: http://www.example.com/page2.php'); and where page2.php then executes some code followed by header('Location: http://www.example.com/page3.php'); etc. This is very bad for user experience, and not very good for managing code either.

In cases where you genuinely need to redirect a user (301 redirect is probably the most common), using header is perfectly acceptable.

share|improve this answer
I don't quite understand the difference in what your saying. Do you mean don't have 2 redirects in a row? –  Howdy_McGee Aug 1 '11 at 16:21
@Howdy_McGee, multiple redirects, or redirects when an include would be more appropriate are issues. It is quite common for beginners to create a php page to do a very specific task, and then instead of using include, trying to use header to pass the user on to another page. I typically avoid headers unless I decide that I need to send a browser a particular header. If a page has moved permanently, use the 301 redirect header and the new location, if a resource wasn't found, send out a 404 header, and possibly populate the page with a sitemap. –  zzzzBov Aug 1 '11 at 17:28
I see, I try to avoid using 'includes' unless i'm including a header.php or footer.php because from my understanding it loads everything inside the 'includes' even php which would mess with load time. I understand what your saying though with the 301 and 404. I think javascript is my base option when redirecting to a custom error page though like the poster above stated. –  Howdy_McGee Aug 1 '11 at 17:33
@Howdy_McGee, includes are the best way to make PHP modular. They're very quick. If you're making an entire library, you should be putting classes and functions into their own scripts, or at least boxing them into groups of similar functions. If you're mixing display logic with business logic, then you should read up on the MVC (Model View Controller) paradigm, because mixing view with controller is a bad idea. –  zzzzBov Aug 1 '11 at 18:12
add comment

It isn't bad. However you could add 301 response code to make it more better, it is also better for Google to determine he should not visit that "old" site anymore.

Header( "HTTP/1.1 301 Moved Permanently" ); 
share|improve this answer
Well what if it is just a simple "do some code then redirect" instead of a permanent redirect? –  Howdy_McGee Aug 1 '11 at 16:23
so use it before this header and after that put there this code (2nd line) –  genesis Aug 1 '11 at 16:41
add comment

This is the accepted method for PHP-based redirection. If you can accomplish the redirect prior to PHP script execution, then you should - through .htaccess or server-level aliasing.

Check out the manual on header: http://php.net/manual/en/function.header.php

share|improve this answer
add comment

It's always sensible to avoid using header() internally because sometimes headers are already called. JavaScript redirects are absolutely fine and are used systematically in a lot of web applications. Browsers and search engines don't discriminate against or dislike JavaScript redirects. A simple example:

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

Or using a variable:

<?php echo '<script type="text/javascript">window.location.href="' . $page . '"</script>'; ?>
share|improve this answer
Well if this is true it seems like the best answer unless somebody wants to argue it. –  Howdy_McGee Aug 1 '11 at 16:50
JavaScript redirects are the worst way to do redirects. You ought to know whether headers have been called or not based on whether you've used a header call or whether you've sent any output with echo or similar. If you've partially sent output to the user, why would you want to then direct them away from that data? Additionally, any user without JavaScript enabled will not be able to follow the redirect. I would also like to see verification about SEO for JS redirects. –  zzzzBov Aug 1 '11 at 18:10
In complicated web apps (especially those with partial views) you don't always know whether the script has previously used a header() function or not. This is why I said it's usually sensible to avoid all potential errors by using JavaScript. The OP said he wanted to redirect a user - he didn't mention anything about maintaining the use of data, variables or existing PHP functions. And the percentage of users with JavaScript enabled is something ridiculous like 2% (or so Verisign argues - bit.ly/nZ75ql). You can always echo out a noscript tag. –  hohner Aug 1 '11 at 18:16
@Jamie, "you don't always know whether the script has previously used a header() function or not" WTF are you talking about?! Of course you can tell whether headers have been sent. –  zzzzBov Aug 1 '11 at 20:21
add comment

Just use

share|improve this answer
It does not explain why or if it's bad –  genesis Aug 1 '11 at 15:50
Nothing bad about it. It's just the accepted method for PHP. –  Mark Aug 1 '11 at 15:52
This is what i'm trying to avoid... –  Howdy_McGee Aug 1 '11 at 15:53
What I think that the writer of the examples tries to say is that what he wrote are just examples just like he said. In most of the cases in the example it isn't necessary to do a redirect. –  Mark Aug 1 '11 at 15:57
add comment

Your Answer


