Home > Article > Backend Development > php login verification jump page
In web development, login verification is a common function. After the user enters their username and password on the login page, the server needs to verify whether the information is correct. If the information entered is correct, the server will allow the user to access pages that require a login. Otherwise, the server will deny the user access, or jump to the login page to re-enter the user name and password. In PHP, it is relatively simple to implement login verification and jump to a specified page. You only need to use some PHP related functions and techniques to achieve it. This article will introduce how to use PHP to implement login verification and jump to the specified page.
First, we need to create a login page and a verification page. The login page requires a form where the user needs to enter their username and password. The verification page requires a PHP script to verify whether the information entered by the user is correct. In this example, we simplify the process first. The username and password are written in the PHP script, and then extended to the database.
The following is a simple login page example:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>登录</h1> <form method="post" action="login.php"> <label for="username">用户名:</label><input type="text" name="username"><br/> <label for="password">密码:</label><input type="password" name="password"><br/> <input type="submit" value="登录"> </form> </body> </html>
In this page, we set up a form to let the user enter their username and password. The action attribute of the form is set to "login.php", which means that the form data is submitted to the PHP script named login.php through POST.
The following is an example of a simple verification page:
<?php session_start(); // 启动会话 // 确定正确的用户名和密码 $valid_username = 'user'; $valid_password = 'password'; // 获取用户输入的用户名和密码 $username = isset($_POST['username']) ? $_POST['username'] : ''; $password = isset($_POST['password']) ? $_POST['password'] : ''; // 验证用户名和密码 if ($username === $valid_username && $password === $valid_password) { $_SESSION['username'] = $username; // 记录登录状态 header('Location: main.php'); // 跳转到主页面 } else { echo '用户名或密码错误。'; } ?>
In this page, we first start a PHP session to record the user's status during the login process. Next, we define a correct username and password. This is simply written in the code. In actual applications, it needs to be queried from the database. We then take the username and password entered by the user and compare it to the correct username and password. If the username and password match, use the $_SESSION array to record the username (for later page verification) and jump to the page named main.php. Otherwise, output an error message and stay on the current page.
During the login process, we record the user's username information. In pages that require login status verification, we can use this information to determine whether the user is logged in. If the user is already logged in, the content of the page can be displayed directly; otherwise, the user needs to jump to the login page and log in again.
The following is a simple example:
<?php session_start(); // 启动会话 if (!isset($_SESSION['username'])) { header('Location: login.php'); // 跳转到登录页面 die(); //结束执行当前命令 } echo '欢迎你,' . $_SESSION['username'] . '。'; ?>
In this page, we first start the session. Then, we use the isset function to determine whether the $_SESSION array contains user name information. If it is not included, use the header function to jump to the login page. If included, the required page content can be displayed.
The following is a complete example, including login page, verification page and main page:
Login page (login.php) :
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>登录</h1> <form method="post" action="login.php"> <label for="username">用户名:</label><input type="text" name="username"><br/> <label for="password">密码:</label><input type="password" name="password"><br/> <input type="submit" value="登录"> </form> </body> </html>
Authentication Page (main.php) :
<?php session_start(); // 启动会话 if (!isset($_SESSION['username'])) { header('Location: login.php'); // 跳转到登录页面 die(); //结束执行当前命令 } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>主页面</title> </head> <body> <h1>欢迎你,<?php echo $_SESSION['username']; ?>。</h1> <p>这里是主页面。</p> </body> </html>
In this example, we first started the session in each page. When the user successfully logs in (that is, enters the correct username and password in the login.php page) the username information will be recorded in the session array $_SESSION. In the main page, we check whether the session array $_SESSION contains the username. If it is not included, use the header function to jump to the login page (login.php) and log in again.
When the user enters the correct login information, the login page (login.php) will immediately jump to the main page (main.php) and display the welcome message. If the user is not logged in or has an invalid login, it will immediately jump to the login page (login.php) to log in again.
Summary
It is very easy to implement login verification jump page in PHP. First, you need to create a login page and verification page. Then the verification page needs some basic PHP code to verify whether the information entered by the user is correct. If it is correct, the user status will be recorded in $_SESSION and jump to the specified page. In the page that needs to verify the login status, just use the record information in $_SESSION. In this way, you can ensure that all pages that require login verification are only accessed by logged-in users, ensuring the security and functionality of the website.
The above is the detailed content of php login verification jump page. For more information, please follow other related articles on the PHP Chinese website!