Home  >  Article  >  Backend Development  >  How to use header() function in php to jump to web page

How to use header() function in php to jump to web page

PHPz
PHPzOriginal
2023-03-24 16:11:532010browse

PHP is a commonly used server-side scripting language. Its function is to generate dynamic web content. In websites, we often need to jump users, such as jumping to the homepage after successful login, or jumping to a specified page. In PHP, you can use the header() function to jump to web pages.

Header() function usage

The header() function is a function in PHP used to process HTTP header information. It can send HTTP header information to the client. This enables functions such as jumps. Its usage is as follows:

header(string $string[, bool $replace = true[, int $http_response_code = 0]])

Among them, the $string parameter is required. It represents the HTTP header information to be sent, which can be a single string or a key-value pair. For example, to redirect the user to http://www.example.com, you can use the following code:

header("Location: http://www.example.com");

This statement will set the Location field in the HTTP header information to http://www .example.com to achieve the jump.

It should be noted that if the header() function is not called before actual output (such as echo), then it will take effect. That is, if there is any output before the header() function, the function will fail and return a warning.

Example

The following is a simple example that demonstrates how to implement web page jump in PHP. Suppose there is a login.php page. When the user enters the correct username and password, it jumps to the welcome.php page.

login.php page code is as follows:

bfc3accdc0f81d6c75181c3df950bd56
c706572c78381e16851918e8b1c4330c
    用户名:46a620ee67c45579c3756a2e4b58b9290c6dc11e160d3b678d68754cc175188a
    密码:2c3c7aebb2ed41195f078969f07512640c6dc11e160d3b678d68754cc175188a
    b684fe7f9e6b0652af06d07b6ecabde5
f5a47148e367a6035fd7a2faa965022e

welcome.php page code is as follows:

54edc68b1a04243e64fcf29db8697815

When the user enters the correct user name and password on the login.php page, It will jump to the welcome.php page and output the prompt message "Welcome to this site!"

Summary

You can use the header() function to implement web page jump in PHP. This function can set the Location field in the HTTP header information to implement the jump. It should be noted that there cannot be any actual output before the header() function, otherwise the function will fail and return a warning.

The above is the detailed content of How to use header() function in php to jump to web page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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