Home  >  Article  >  Backend Development  >  Detailed explanation of using php to implement simple user login function

Detailed explanation of using php to implement simple user login function

PHPz
PHPzOriginal
2023-04-03 15:00:571997browse

PHP is a widely used server-side scripting language that is widely used to develop web applications, in which implementing user login functionality is a basic requirement. In this article, we will introduce how to implement a simple user login function using PHP.

First, we need a login page. Here is a very simple HTML login page:

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login Form</h2>
    <form method="post" action="login.php">
        <label>Username:</label><br>
        <input type="text" name="username"><br>
        <label>Password:</label><br>
        <input type="password" name="password"><br><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>

The page includes a form for entering username and password, and a login button. We will use the data from this form to authenticate the user and send it to the login php page, i.e. login.php.

Let’s take a look at the PHP code required for the login.php page.

<?php
session_start(); //启动Session

if($_SERVER["REQUEST_METHOD"] == "POST"){
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    //检查用户输入是否正确,这里我们使用假数据进行比较
    if($username === "admin" && $password === "123456"){
        $_SESSION["username"] = $username; //设置session变量
        header("Location: welcome.php"); //跳转到欢迎页面
        exit();
    }
    else {
        $error = "Invalid username or password";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login Result</title>
</head>
<body>
    <?php
    if(isset($error)){
        echo $error;
    }
    ?>
</body>
</html>

In this PHP page, we first start the session, check the request method, determine whether it is a POST method, and obtain the user name and password entered in the form. Here we use fake data for comparison to verify whether the user identity is correct.

If authentication is successful, we set a session variable called "username" and redirect the user to the welcome page. Otherwise, we will display an error message.

Next, let’s see how to use session variables to manage the user’s login status. We will create a simple page called welcome.php that just displays the welcome message and checks if the user is logged in.

<?php
session_start(); //启动Session

if(!isset($_SESSION["username"])){
    header("Location: login.php"); //如果未登录,则返回到登录页面
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h2>Welcome, <?php echo $_SESSION["username"]; ?></h2>
    <p>You have successfully logged in.</p>
</body>
</html>

We first started the session, and then checked whether the session variable "username" has been set. If not, we redirect the user back to the login page. Otherwise, we welcome the user and display a successful login message.

The above is all the code we need to create a simple user login function! Through this example, you can see that implementing a login page using PHP is very simple and straightforward.

Remember to strengthen security and data verification, and use encryption algorithms to protect users' sensitive information in actual use.

The above is the detailed content of Detailed explanation of using php to implement simple user login function. 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