How to use header function to redirect page in PHP

王林
Release: 2023-06-26 13:02:02
Original
1296 people have browsed it

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:

Copy after login

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:

Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!