How to use php to write a successful login jump

PHPz
Release: 2023-04-19 14:10:14
Original
1051 people have browsed it

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:

<!DOCTYPE html>
<html>
<head>
    <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>
</head>
<body>
    <div class="login">
        <h2>登陆页面</h2>
        <form action="process.php" method="POST">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码">
            <button type="submit">登陆</button>
            <?php
                if(isset($_GET[&#39;error&#39;])){
                    if($_GET[&#39;error&#39;] == &#39;empty&#39;){
                        echo "<p class=&#39;error&#39;>请输入所有字段!</p>";
                    }
                    elseif ($_GET['error'] == 'wrong') {
                        echo "<p class=&#39;error&#39;>请输入正确的用户名和密码!</p>";
                    }
                }
            ?>
        </form>
    </div>
</body>
</html>
Copy after login

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();
    }
?>
Copy after login

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.

<!DOCTYPE html>
<html>
<head>
    <title>欢迎页</title>
</head>
<body>
    <h1>欢迎</h1>
    <p>您已经成功登陆, <?php echo $_SESSION[&#39;username&#39;]; ?>!</p>
    <a href="logout.php">注销</a>
</body>
</html>
Copy after login

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

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!

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