Home  >  Article  >  Backend Development  >  How to implement php login function

How to implement php login function

藏色散人
藏色散人Original
2020-07-03 11:02:402421browse

php method to implement the login function: 1. Connect and create a database; 2. Use the layui framework to determine whether the username and password input is empty; 3. Create "login.php" to determine the username and password Correctness; 4. Check the login effect.

How to implement php login function

php login function implementation

It is natural to start with the simplest function. My first task was to perform a login operation, which was actually not as simple as I imagined.

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);

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

76c82f278ac045591c9159d381de2c57
9fd01892b579bba0c343404bcccd70fb
6012274a928fd1ff093899c3409321b22cacc6d41bbb37262a98f745aa00fbf0
88499d69c5381e3453b4acdedf0d3afc
93f0f5c25f18dab9d176bd4f6de5d30e
    a80eb7cbb6fff8b0ff70bae37074b813
    b2386ffb911b14667cb8f0f91ea547a7注册登录6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
84fb051152a75de76fa5ac89062af38c
    function InputCheck()
    {
        if (Login.username.value == "")
            {
             alert("请输入用户名!");
             Login.username.focus();
             return (false);
             }
        if (Login.password.value == "")
             {
               alert("请输入密码!");
               Login.password.focus();
            return (false);
             }
         }
2cacc6d41bbb37262a98f745aa00fbf0
70a5e3b3668c161d310d45fa7a2257f7
0ec150d2f91181917f121b660f33619e
    2d70337eb0283dd5aaf2194c77dbf61d
        872e068b23cab27ce3d7d18c4dd3f104
            b1b43e4f54f16730e172a0d9239f7c55
                87fbc25c76eb4ee38855c6ad2a573260
                    c1a436a314ed609750bd7c7d319db4da注册登录系统2e9b454fa8428549ca2e64dfac4625cd
                94b3e26ee717c64999d7867364b1b4a3
                f32b48428a809b51f04d3228cdf461fa
                9b8a638eb0a123998e0dcedf43dbbeda
                    ad29588debfb592956c4c08606e16152用户名8c1ecd4bb896b2264e0711597d40766c
                    5e67097ac584553f05ee45d4bc8bddbb
                        45c93afb097a11074ed2d2691dc813d3
                    94b3e26ee717c64999d7867364b1b4a3
                94b3e26ee717c64999d7867364b1b4a3
                9b8a638eb0a123998e0dcedf43dbbeda
                    ad29588debfb592956c4c08606e16152密    码8c1ecd4bb896b2264e0711597d40766c
                    5e67097ac584553f05ee45d4bc8bddbb
                        a267a0ef0508006df0c9ade2c3de35df
                    94b3e26ee717c64999d7867364b1b4a3
                94b3e26ee717c64999d7867364b1b4a3
                9b8a638eb0a123998e0dcedf43dbbeda
                    955259a2ec89d2af493d489c4b11a016
                        954d795e0028c9bbbb251c7e8c6d985e
                        28ea0d221fdf47833a4d698c7b43d4ea
                    94b3e26ee717c64999d7867364b1b4a3
                94b3e26ee717c64999d7867364b1b4a3
            f5a47148e367a6035fd7a2faa965022e
        94b3e26ee717c64999d7867364b1b4a3
    94b3e26ee717c64999d7867364b1b4a3
94b3e26ee717c64999d7867364b1b4a3
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

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>";
    };

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.

Recommended: "PHP Tutorial"

The above is the detailed content of How to implement php 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