#59: Redirecting Users to Different Pages

Redirecting users to new pages is a fact of life when programming dynamic websites. The primary reason is that you often need to redirect a user after making session state changes. For example, when you add an item to a shopping cart, you want the user to go to a page such as the current shopping cart, and you don't want to add another one of the same item to the cart again if you reload that shopping cart page. Most sites achieve this with a script that processes the state change and then redirects to the page that they want the user to see.

There are essentially two ways to do it. The first and preferred way is to use the HTTP Location header, like this:

<?
    header("Location: new_page.php");
?>

The header() ...

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.