Home >Backend Development >PHP Problem >PHP detects whether account password matches

PHP detects whether account password matches

尚
Original
2019-10-29 11:31:372597browse

PHP detects whether account password matches

PHP detects whether the account password matches:

Recommended: php server

HTML code:

<style>
    #login{width: 250px;height: 120px;background-color: aquamarine;position: fixed;top: 0px;left: 0px;right: 0px;bottom: 0px;margin: auto;padding:50px;text-align: left;}
</style>
<script type="text/javascript" src="__PUBLIC__/js/jquery-3.2.1.js"></script>
<script type="text/javascript">
function rusername(obj){
    var username=$(obj).val();
    $.post("{:U(&#39;Login/lusername&#39;)}",{username:username},function(data) {
        $("#username").html(data)
    })
}
function rpassword(obj){
    var password=$(obj).val();
    $.post("{:U(&#39;Login/lpassword&#39;)}",{username:password},function(data) {
        $("#password").html(data)
    })
}
</script>
<div id="login">
    <form action="__ACTION__" method="post">
    <p><input name="username" type="text" value="" size="25" placeholder="填写用户名" onblur="rusername(this);"/><br/><span id="username" style="font-size:12px;color:red;"></span></p>
    <p><input name="password" type="password" value="" size="25" placeholder="输入密码" onblur="rpassword(this);"/><br/><span id="password" style="font-size:12px;color:red;"></span></p>
    <p><input name="submit" type="submit" value="登录"/>
               
               
               
        <input name="submit" type="submit" value="注册"/>
    </p>
    </form>
</div>

PHP code:

<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function login(){
        if($_POST){
            $where[&#39;username&#39;]=$_POST[&#39;username&#39;];
            $where[&#39;password&#39;]=md5("php100".$_POST[&#39;password&#39;]);
            $users=M(&#39;users&#39;)->where($where)->select();
            if($users){
                $_SESSION[&#39;id&#39;]=$users[&#39;id&#39;];
                $_SESSION[&#39;user_shell&#39;]=md5($users[&#39;username&#39;].$users[&#39;password&#39;]);
                $this->redirect(&#39;Index/index&#39;);
            }else{
                echo "<script type=&#39;text/javascript&#39;>alert(&#39;用户名或密码错误&#39;);window.history.go(-1);</script>";
            }
        }else{
            $this->display();
        }
    }
    public function lusername(){
        $where[&#39;username&#39;]=I(&#39;username&#39;);
        $users=M(&#39;users&#39;)->where($where)->select();
        if(!$users){
            echo "用户名错误";
        }
    }
    public function lpassword(){
        $where[&#39;password&#39;]=I(&#39;password&#39;);
        $users=M(&#39;users&#39;)->where($where)->select();
        if(!$users){
            echo "密码错误";
        }
    }
}

The above is the detailed content of PHP detects whether account password matches. 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