Rumah > pembangunan bahagian belakang > tutorial php > PHP如何编写客户端扫描二维码登录PC端后台功能

PHP如何编写客户端扫描二维码登录PC端后台功能

小云云
Lepaskan: 2023-03-21 15:58:01
asal
2423 orang telah melayarinya

本文主要和大家分享PHP如何编写客户端扫描二维码登录PC端后台功能,本人也是第一次写这个功能,受下方博主博客启发机demo完成此功能,自己想重新归纳一下此代码,以防忘记。 

html页面

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" href="login/css/normalize.css">
    <link rel="stylesheet" href="login/css/style.css">
</head>

<body>
    <p class="top">
        <img src="login/img/1.png" alt="">

    </p>
    <p class="middle">

        <p class="login_p">
            <img src="login/img/3.png" alt="" class="change_login">
            <img src="login/img/9.png" alt="" class="change_txt">
            <p class="change_p">
                <p class="con_top">
                    <img src="login/img/8.png" alt="">
                    <span>管理员登录</span>
                </p>
                <p class="con_bottom">
                    <p id="account">

                        <br>
                        <img src="login/img/6.png" alt="" class="userimg">
                        <input type="text" id="username" placeholder="请输入用户名">
                        <br>
                        <br>
                        <br>
                        <img src="login/img/7.png" alt="" class="passimg">
                        <input type="password" id="password" placeholder="请输入密码">
                        <br>
                        <br>
                        <br>
                        <br>
                        <p id="btnSubmit" class="btnSubmit">登录</p>
                        <br>
                    </p>
                    <p id="code" style="display: none;">
                        <p class="code_p">
                            <img src="" alt="" id="qrcodeimg" width="144px" height="145px">
                        </p>
                        <p class="code_txt">
                            <img src="login/img/18.png" alt="">
                            <p>
                                <p>  打开APP</p>
                                <p>  扫一扫登录</p>
                            </p>
                        </p>
                    </p>
                    <p class="hint">
                        <span>二维码有效期2分钟</span>
                    </p>
                </p>
            </p>
        </p>
    </p>

</body>

<script src=&#39;login/js/jquery-1.9.0.min.js&#39;></script>
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script src=&#39;login/js/index.js&#39;></script>
<script src=&#39;static/h-ui.admin/js/login.js&#39;></script>


</html>
Salin selepas log masuk

js页面

$.ajax({            type: "GET",
            url: "er/qrcodelogin/getqruuid.php",            data: {},
            success: function (result) {
                console.log(result);                var data = JSON.parse(result);                // console.log(data.code);
                if (data.code == 1) {
                    $("#qrcodeimg").attr(&#39;src&#39;, &#39;http://qr.liantu.com/api.php?text=&#39; + result);


                    console.log(data.data);                    var min=0;

                    interval1= setInterval(function () {                        min=min+1000;

                        $.ajax({                            type: "GET",
                            url: "er/qrcodelogin/checkqruuid.php",                            data: {&#39;qruuid&#39;: data.data},
                            success: function (result) {                                    min=min+1000;                                    if(min==36000){
                                        console.log(min);
                                        alert("二维码已过期!");
                                        clearInterval(interval1);
                                        location.replace(location.href);                                        min=1000;
                                        interval1();
                                    }else{
                                        console.log(min);                                        var data = JSON.parse(result);                                        if (data.code == 1) {                                            // alert(&#39;扫码成功(即登录成功),进行跳转.....&#39;);
                                            //停止轮询
                                            clearInterval(interval1);
                                            location.href="index.html";                                            //TODO 拿到需要的信息 然后跳转什么的
                                        }else if(data.code == 300){
                                            alert(&#39;无此后台用户!&#39;);                                            //停止轮询
                                            clearInterval(interval1);
                                            location.href="login.html";
                                        }
                                    }

                            }
                        });

                    }, 1000);//1秒钟  频率按需求
                }
            }
        });
Salin selepas log masuk

getqruuid.php页面

<?php/**
 * 用于前端获取qruuid(二维码唯一ID)使用
 *
 * Created by PhpStorm.
 * User: caohan
 * Date: 2017/10/15
 * Time: 下午2:59
 */require(&#39;config.php&#39;);//生成随机的UUID 用于二维码显示的内容 和 绑定用$qruuid = substr(md5(uniqid(mt_rand(), true)), 0, 15);//生成uuid//将生成的随机数保存至数据库$sql="INSERT into t_pc_code(randnumber) values (&#39;$qruuid&#39;)";$query = sqlsrv_query($conn, $sql);if (!$query) {       die(print_r(sqlsrv_errors(), true));
}$arr = [&#39;code&#39;=>1, &#39;msg&#39; => &#39;生成qruuid成功&#39;,&#39;data&#39;=>$qruuid];echo json_encode($arr);exit();
Salin selepas log masuk

checkqruuid.php页面

<?phpsession_start();
/**
 * Created by PhpStorm.
 * User: caohan
 * Date: 2017/10/15
 * Time: 下午3:17
 */require(&#39;config.php&#39;);
 // $mysqli = new mysqli($db_host, 
 $db_user, $db_pwd, $db_name);
 // if (mysqli_connect_error())
 //     echo mysqli_connect_error();
 // $mysqli->set_charset("utf8");$qruuid = 
 $_GET[&#39;qruuid&#39;];$sql = "SELECT * from t_pc_code where randnumber=&#39;" . 
 $qruuid . "&#39;";$result = sqlsrv_fetch_array(sqlsrv_query($conn, $sql));if (!$result) 
 {       die(print_r(sqlsrv_errors(), true));
}if (!is_null($result[&#39;username&#39;])){    $nowusername=$result[&#39;username&#39;];    
$sql2="SELECT * from t_webuser where username = &#39;$nowusername&#39; and isDeleted=0";    
$result2 = sqlsrv_query($conn, $sql2);    if($row=sqlsrv_fetch_array($result2)){       
 $_SESSION[&#39;sydsdj_name&#39;]=$row[&#39;username&#39;];       
  $_SESSION[&#39;sydsdj_roleType&#39;]=$row[&#39;roleType&#39;];       
  $_SESSION[&#39;sydsdj_orgId&#39;]=$row[&#39;orgId&#39;];       
   $_SESSION[&#39;sydsdj_orgName&#39;]=$row[&#39;orgName&#39;];      
    $_SESSION[&#39;sydsdj_manage_id&#39;]=$row[&#39;id&#39;];       
     $arr = [&#39;code&#39; => 1, &#39;msg&#39; => &#39;登录成功&#39;, &#39;data&#39; => $result];
    }else{        $arr = [&#39;code&#39; => 300, &#39;msg&#39; => &#39;账号未注册!&#39;, &#39;data&#39; => $result];
    }

}else{    $arr = [&#39;code&#39; => 500, &#39;msg&#39; => &#39;qruuid暂时未被绑定&#39;,&#39;data&#39;=>$qruuid];
}echo json_encode($arr);exit();?>
Salin selepas log masuk

写给客户端存储用户账号的接口

<?php// 指定允许其他域名访问  
header(&#39;Access-Control-Allow-Origin:*&#39;);require(&#39;config.php&#39;);
$username = $_REQUEST[&#39;username&#39;];$randnumber = $_REQUEST[&#39;randnumber&#39;];
$result = sqlsrv_query($conn,"UPDATE t_pc_code set username=&#39;
$username&#39; where randnumber= &#39;$randnumber&#39;");if (!$result) 
{    die(print_r(sqlsrv_errors(), true));   
 $arr = [&#39;code&#39; => 0, &#39;msg&#39; => &#39;sy&#39;];

}else{    $arr = [&#39;code&#39; => 1, &#39;msg&#39; => &#39;sy&#39;];
}echo json_encode($arr);?>
Salin selepas log masuk

相关推荐:

通过扫描二维码打开app的实现代码

微信扫描二维码登录网站代码_PHP教程

php实现扫描二维码根据浏览器类型访问不同下载地址,php浏览器类型_PHP教程

Atas ialah kandungan terperinci PHP如何编写客户端扫描二维码登录PC端后台功能. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan