search
HomeBackend DevelopmentPHP ProblemHow to use php to implement user login

php method to implement user login: 1. Create a get.php file to obtain user login information; 2. Create a login.php file to implement the login function; 3. Create a logout.php file. It can be used to implement the user logout function.

How to use php to implement user login

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to use php to achieve user login?

Put it on your daily PHP learning routine to record it

I used the bootstrap framework, so this interface should look better

Login page:
How to use php to implement user login
You must enter the correct username, password, and verification code to successfully log in. Otherwise, the following prompt will appear
How to use php to implement user login
How to use php to implement user login
After successful login, the login and registration options switch to User nickname and logout options:
How to use php to implement user login

After successful login:
How to use php to implement user login

Select logout:
How to use php to implement user login

##HTML Code:

  <link>
 	<style>
          li.toggle{
              display: block;
           }
          li.toggle1{
              display: none;
            }
        </style>
        
  • 登录
  •         
  • 注册
  •         
  •         
  • 注销
  • //模态框

        

            

                

                                     

    请登录

                             
                    

                        

                                                     

                                                         

                                             

                                                     

                                                         

                                             

                                                     

                                                         

                            

                                How to use php to implement user login                         

                                             

                                                 

                                     

                                                              

                
                  JS code:

    <script></script>
    <script></script>
    <script>
        function toggle(){
            $.getJSON("php/get.php",function (res) {
                if(res[&#39;flag&#39;]){
                    $(&#39;li.toggle&#39;).hide();
                    $(&#39;li.toggle1&#39;).show();
                    $(&#39;#nicheng&#39;).html(res[&#39;nc&#39;])
                }else {
                    $(&#39;li.toggle&#39;).show();
                    $(&#39;li.toggle1&#39;).hide();
                }
            });
        }
        toggle();
        $(&#39;#form_login&#39;).submit(function (e) {
            e.preventDefault();
            var data=$(&#39;#form_login&#39;).serialize();
            $.getJSON(&#39;php/login.php&#39;,data,function (res) {
                if(res==3){
                    toggle();
                    $(&#39;#loginer&#39;).modal(&#39;hide&#39;);
                }else if(res==2){
                    $(&#39;#info&#39;).html(&#39;用户名或密码有误&#39;)
                }else {
                    $(&#39;#info&#39;).html(&#39;验证码有误&#39;)
                }
            })
        });
        $(&#39;#logout&#39;).click(function () {
            confirm(&#39;确定要注销?&#39;);
            $.getJSON(&#39;php/logout.php&#39;,function (res) {
                if(res)toggle();
            })
        })
        </script>
    connect.php (connect to database):

    <?php $link=new PDO("mysql:host=localhost;port=3306;dbname=db","root","");
    $link->query("set names utf8");
    get.php (get user login information):

    <?php include_once ("connect.php");
    session_start();/*开启会话*/
    if (isset($_SESSION[&#39;username&#39;])){
       /* 判断用户会话里用户名是否存在,即用户是否登录*/
        $json[&#39;nc&#39;]=$_SESSION[&#39;nc&#39;];/*把昵称存起来,一会返回给首页*/
        $json[&#39;flag&#39;]=true;/*用户已经登录,标志flag为true*/
    }else
        $json[&#39;flag&#39;]=false;/*用户已经登录,标志flag为true*/
    echo json_encode($json);/*返回json*/
    login.php (login):

    <?php include_once ("connect.php");
    session_start();/*开启会话*/
    $user=$_GET[&#39;username&#39;];/*获取登录表单提交过来的数据*/
    $pwd=$_GET[&#39;pwd&#39;];
    $yzm=$_GET[&#39;yzm&#39;];
    if($yzm==$_SESSION[&#39;vCode&#39;]){/*当用户输入的验证码和图片验证码相同时*/
        $result=$link->query("select * from `user` where username='$user' and pwd='$pwd'");
        $link = null;
        $row = $result->fetch();/*读取从数据库获取的数据*/
        if ($row) {/*如果数据存在,即用户登录成功*/
            $_SESSION['username'] = $row['username'];
            /*将用户名和昵称存在服务器,可以多个页面使用*/
            $_SESSION['nc'] = $row['nc'];
            $flag=3;
        }else{/*用户名或密码错误*/
            $flag=2;
        }
    }else{/*验证码输入错误*/
        $flag=1;
    }
    echo $flag;
    logout.php (logout):

    <?php session_start();
    unset($_SESSION[&#39;username&#39;]);//销毁用户名
    unset($_SESSION[&#39;nc&#39;]);//销毁昵称
    echo json_encode(true);//返回结果
    Recommended learning: "

    PHP Video Tutorial"

    The above is the detailed content of How to use php to implement user login. 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
    ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

    The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

    PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

    The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

    PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

    Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

    PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

    The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

    PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

    The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

    OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

    The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

    PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

    The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

    PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

    The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

    See all articles

    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

    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.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment