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.
- Build a login page and a verification 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:
<meta charset="UTF-8"> <title>登录页面</title> <h1 id="登录">登录</h1>
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.
- Determine whether the user is logged in
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.
- Complete example
The following is a complete example, including login page, verification page and main page:
Login page (login.php) :
<meta charset="UTF-8"> <title>登录页面</title> <h1 id="登录">登录</h1>
Authentication Page (main.php) :
<?php session_start(); // 启动会话 if (!isset($_SESSION['username'])) { header('Location: login.php'); // 跳转到登录页面 die(); //结束执行当前命令 } ?> <meta charset="UTF-8"> <title>主页面</title> <h1 id="欢迎你-php-echo-SESSION-username">欢迎你,<?php echo $_SESSION['username']; ?>。</h1> <p>这里是主页面。</p>
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)