In PHP, using the header function to redirect pages is a simple and effective method. The header function allows us to add a Location header directly to the HTTP response header for redirection on the client's browser.
The following is a simple example of how to use the header function to redirect a page:
In this example, we use the header function to send a redirect request to the browser. At this point, we need to specify the redirect target URL, that is, the page URL we want the browser to redirect to. In this case, we redirect the browser to the https://www.example.com page.
Please note that we also use the exit() function after the header function. This function will exit the current script and prevent further execution. Failure to use the exit() function will cause the script to continue executing after redirection, causing other errors or unexpected results.
You can specify a relative path or an absolute path when redirecting. For example, in the current website, we can modify the header function to:
In this case, we use a relative path to specify another page newpage.php in the current website. When the execution result of the PHP code is this redirect, the browser will automatically jump to this page.
Summary:
Using the header function to redirect pages is a convenient, simple and effective way. It should be noted that when using the header function, it must be placed before any output and use the exit() function to exit the current script. Additionally, when using relative URLs, make sure they are valid within the current website.
The above is the detailed content of How to use header function to redirect page in PHP. For more information, please follow other related articles on the PHP Chinese website!