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…
http://tinsology.net/2009/06/creating-a-secure-login-system-the-right-way/
The snippet you provided is referring to issues where
page1.phpmight execute some code followed byheader('Lodation: http://www.example.com/page2.php');and wherepage2.phpthen executes some code followed byheader('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
headeris perfectly acceptable.