PHP: redirect a user to another page

This snippet redirects the user to another web page. Note that a redirect must be called before any HTML output has been sent to the browser. Note also that the script is exited after the redirect header has been sent to avoid further processing by the script (which can lead to unexpected behaviour). The call to header() can also be wrapped inside the call to exit() as an alternative. The URL should technically be absolute, but relative URLs are usually accepted too. Most frameworks have their own method for redirecting; below we look at the basic PHP way.

<?php // Common method header('Location:'); exit; // Relative URLs work too header('Location: index.html'); exit; // Alternative method, wrapping call to header() in exit() exit(header('Location:'));