Home > Backend Development > PHP Tutorial > Relevant code analysis for php to implement login function

Relevant code analysis for php to implement login function

jacklove
Release: 2023-03-31 16:40:01
Original
1529 people have browsed it

Naturally, I started with the simplest function. My first task was to perform a login operation, but it was not as simple as I thought.

1. The first thing is to connect and create the database.

I wrote this part in model.php

$userName='root';
$passWord='';
$host='localhost';
$dataBase='login';
//创建连接
$conn=mysqli_connect($host,$userName,$passWord,$dataBase);
Copy after login

2. To write the front-end page, in order to become proficient in the front-end framework, I used The layui framework makes the interface, and there is a js code in front of it to determine whether the username and password input is empty

<!DOCTYPE html>
<html lang="en">
<script src="layui.js";></script>
<link rel="stylesheet" href="layui.css" ;>
<head>
    <meta charset="UTF-8">
    <title>注册登录</title>
</head>
<script language=JavaScript>
    function InputCheck()
    {
        if (Login.username.value == "")
            {
             alert("请输入用户名!");
             Login.username.focus();
             return (false);
             }
        if (Login.password.value == "")
             {
               alert("请输入密码!");
               Login.password.focus();
            return (false);
             }
         }
</script>
<body style="background: #1E9FFF">
<p style="position: absolute; left: 50%; top: 50%;width: 500px; margin-left:-250px; margin-top: -200px">
    <p style="background: #FFFFFF; padding: 20px;border-radius: 4px;box-shadow: 5px 5px 20px #444444" >
        <p class="layui-form">
            <form action="login.php" method="post" name="Login" onsubmit="return InputCheck()">
                <p class="layui-form-item" style="color: gray">
                    <h2>注册登录系统</h2>
                </p>
                <hr>
                <p class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <p class="layui-input-block">
                        <input type="text" name="username" id="username" placeholder="用户名" autocomplete="off" class="layui-input">
                    </p>
                </p>
                <p class="layui-form-item">
                    <label class="layui-form-label">密    码</label>
                    <p class="layui-input-block">
                        <input type="password" name="password" id="password" placeholder="密码" autocomplete="off" class="layui-input">
                    </p>
                </p>
                <p class="layui-form-item">
                    <p class="layui-input-block";>
                        <input type="submit" value="登录" class="layui-btn">
                        <input type="button" value="注册" class="layui-btn">
                    </p>
                </p>
            </form>
        </p>
    </p>
</p>
</body>
</html>
Copy after login

3.login.php is used to determine the correctness of the username and password. I read about this on the Internet There are many methods, all kinds of. Before I encountered any obstacles, I decided to use a simple form first, which is to use SQL statements to query the result set of user name and password. If the result set is empty, the user does not exist.

<?php
//数据库连接
require_once &#39;model.php&#39;;
//从登录页接受来的数据
$name=$_POST[&#39;username&#39;];
$pwd=$_POST[&#39;password&#39;];
$sql="select id,username,password from user where username=&#39;$name&#39; AND password=&#39;$pwd&#39;;";
$result=mysqli_query($conn,$sql);
$row=mysqli_num_rows($result);
if(!$row){
        echo "<script>alert(&#39;密码错误,请重新输入&#39;);location=&#39;login.html&#39;</script>";
    }
    else{
        echo "<script>alert(&#39;登录成功&#39;);location=&#39;123&#39;</script>";
    };
Copy after login

4. File directory

5. Effect



6. Summary

Overall it is not particularly difficult because the function is very simple. The next task is to add the registration function and verification code function.

This article explains the relevant code of php to implement the login function. For more related content, please pay attention to the php Chinese website.

Related recommendations:
Explanation of JavaScript-related content

Explanation of the basics of HTML

$.ajax php practical tutorial automatically loads more articles when you pull down the principle explanation

The above is the detailed content of Relevant code analysis for php to implement login function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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