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

PHPz
Release: 2023-03-24 17:00:01
Original
2223 people have browsed it

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]])
Copy after login

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");
Copy after login

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:

<?php
// 验证用户名和密码,如果正确则跳转到welcome.php
$username = $_POST["username"];
$password = $_POST["password"];

if ($username == "admin" && $password == "123456") {
    header("Location: welcome.php");
} else {
    echo "用户名或密码错误!";
}
?>
<form method="POST">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登录">
</form>
Copy after login

welcome.php page code is as follows:

<?php
echo "欢迎访问本站!";
?>
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template