PHP is a server-side scripting language widely used in network development and can be used for the production of dynamic web pages and the development of Web applications. In website development, the login system is a very common function, and jumping to the specified page after successful login is also an important part of the user experience. This article will introduce how to use PHP to write the function to achieve successful login jump.
1. Create a form page
First, we need to create a login page where users can enter their username and password. To make this page more readable, we can use CSS styles in HTML to beautify the page. Our form page should include the following content:
- Login form;
- A form containing user name and password input boxes;
- Button to submit the form using the POST method;
- Input error message.
The HTML code is as follows:
nbsp;html> <title>登陆页面</title> <style> body{ background-color: #F4F4F4; } .login{ margin: 100px auto; border: 1px solid #ccc; width: 400px; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px #ccc; } input[type=text], input[type=password]{ width: 100%; padding: 10px; margin: 8px 0; display: inline-block; border: 1px solid #ccc; box-sizing: border-box; border-radius: 5px; } button[type=submit]{ background-color: #4CAF50; color: white; padding: 10px 18px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; } button[type=submit]:hover{ background-color: #45a049; } .error{ color: red; } </style> <div> <h2 id="登陆页面">登陆页面</h2> <form> <label>用户名</label> <input> <label>密码</label> <input> <button>登陆</button> <?php if(isset($_GET['error'])){ if($_GET['error'] == 'empty'){ echo "<p class='error'>请输入所有字段!"; } elseif ($_GET['error'] == 'wrong') { echo "<p>请输入正确的用户名和密码!</p>"; } } ?> </form> </div>
2. Verify username and password
Next, we need to verify whether the username and password entered by the user are correct. We can use PHP on the server side to implement this process. First, we need to create a process.php file on the server side. This file will receive the form data and verify that the username and password are correct. If correct, we will redirect the user to another page in this file.
<?php session_start(); if(isset($_POST['username']) && isset($_POST['password'])){ include 'db_connection.php'; $username = $_POST['username']; $password = $_POST['password']; if(empty($username) || empty($password)){ header("Location: index.php?error=empty"); exit(); } else{ $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); if(mysqli_num_rows($result) === 1){ $row = mysqli_fetch_assoc($result); if($row['username'] === $username && $row['password'] === $password){ $_SESSION['username'] = $row['username']; header("Location: welcome.php"); exit(); } } else{ header("Location: index.php?error=wrong"); exit(); } } } else{ header("Location: index.php"); exit(); } ?>
In the above code, we first use the session_start() function to open a new session. We then check if username and password exist in the $_POST array. If present, we import the database connection variables from the db_connection.php file as we need to retrieve the user data from the database. We then use an if statement to check if the entered field is empty. If empty, redirects the user to the original form page and includes the error code in the URL.
If the input field is not empty, we can retrieve the user's record in the database. If we get a record with a matching username and password, we can store the username in $_SESSION and use the header() function to redirect the user to the welcome.php page. If there is no such record in the database, we will redirect the user to the original form page and include the error code in the URL.
3. Create a welcome page
Finally, we need to create a welcome page to which the user will be redirected after successfully logging in. The welcome page can simply display a welcome message, and a button that enables the user to log out and log out.
nbsp;html> <title>欢迎页</title> <h1 id="欢迎">欢迎</h1> <p>您已经成功登陆, <?php echo $_SESSION['username']; ?>!</p> <a>注销</a>
In the welcome page, we use PHP's $_SESSION mechanism to display the current username. We also added a link to the page that implements the logout functionality through the page's logout.php file.
4. Implement the logout function
Finally, we need to use PHP to write the logout.php file to implement the logout function.
<?php session_start(); session_unset(); session_destroy(); header("Location: index.php"); exit(); ?>
In the logout.php file, we first start the session using the session_start() function. We use the session_unset() and session_destroy() functions to clear all session data and redirect the user back to the home page. This will log the user out after clicking on the "Logout" link.
Summary
In this article, we introduced how to use PHP to write a simple system that achieves a successful login jump. By using PHP and MySQL, verify the username and password when the form is submitted, redirect the user to another page after successful verification, and return the user to the original form page when an error is encountered. We also covered how to display the currently logged-in username in the welcome page and add logout functionality in the logout.php file. This is a simple but functional login success redirection system that can provide visitors with a good user experience on your website.
The above is the detailed content of How to use php to write a successful login jump. For more information, please follow other related articles on the PHP Chinese website!

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

This article explores asynchronous task execution in PHP to enhance web application responsiveness. It details methods like message queues, asynchronous frameworks (ReactPHP, Swoole), and background processes, emphasizing best practices for efficien

This article addresses PHP memory optimization. It details techniques like using appropriate data structures, avoiding unnecessary object creation, and employing efficient algorithms. Common memory leak sources (e.g., unclosed connections, global v

This article explores strategies for staying current in the PHP ecosystem. It emphasizes utilizing official channels, community forums, conferences, and open-source contributions. The author highlights best resources for learning new features and a


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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