search
HomeBackend DevelopmentPHP ProblemHow to use php to write a successful login jump

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[&#39;error&#39;])){
                    if($_GET[&#39;error&#39;] == &#39;empty&#39;){
                        echo "<p class=&#39;error&#39;>请输入所有字段!";
                    }
                    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[&#39;username&#39;]) && isset($_POST[&#39;password&#39;])){
        include &#39;db_connection.php&#39;;
        $username = $_POST[&#39;username&#39;];
        $password = $_POST[&#39;password&#39;];
        if(empty($username) || empty($password)){
            header("Location: index.php?error=empty");
            exit();
        }
        else{
            $sql = "SELECT * FROM users WHERE username=&#39;$username&#39; AND password=&#39;$password&#39;";
            $result = mysqli_query($conn, $sql);
            if(mysqli_num_rows($result) === 1){
                $row = mysqli_fetch_assoc($result);
                if($row[&#39;username&#39;] === $username && $row[&#39;password&#39;] === $password){
                    $_SESSION[&#39;username&#39;] = $row[&#39;username&#39;];
                    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[&#39;username&#39;]; ?>!</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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment