夺命雷公狗---PHP---玩转安卓2之扫码进行登录原理

如今的网络扫码进行登录的越来越多了,我们在很多时候也是需要开发到这个功能的,这个功能其实也算不上太难。

我们这次要借助的平台是联图网的二维码api接口

这个接口可以为我们省了不少的事。。。

我们开干,首先在数据库创建一张user的表

然后创建一个az.php的文件,主要用于生成二维码的

<?php
    $link = mysql_connect('localhost','root','') or die('数据库链接失败');
    mysql_query('set names utf8');
    mysql_select_db('xsphp');
    
?>
<DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>二维码</title>
    </head>
    <body>
        <?php
         $randnumber = "";
         for($i=0;$i<8;$i++){
            $randnumber .= rand(0,9);
         }
         
         $sql = "insert into user(randnumber) values('{$randnumber}')";
         mysql_query($sql);
         
         ?>
        <img src="http://qr.topscan.com/api.php?text=<?php echo $randnumber; ?>" width="150px" />
        <input type="hidden" name="randnumber" id="randnumber" value="<?php echo $randnumber; ?>" />
    </body>
    <script>
        function polling(){
            //执行轮询操作
            var xmlHttp;
            
            //为了支持多钟版本浏览器
            if(window.XMLHttpRequest){
                xmlHttp = new XMLHttpRequest();
            }else{
                xmlHtpp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            
            
            xmlHttp.onreadystatechange = function(){ //运行该函数
                if(xmlHttp.status == 200 && xmlHttp.readyState == 4){ //请求完成并且成功返回
                    result = xmlHttp.responseText; //接收服务器返回的文本内容。
                    if(result == 'true'){
                        window.location.href ='hello.php'; //如果成功跳转到的页面
                    }
                }
            }
            
            
            randnumber = document.getElementById("randnumber").value; //获取到randnumber的值
            xmlHttp.open("GET","polling.php?randnumber="+randnumber,true); //发送的地址
            xmlHttp.send();  //发送请求到http服务器并接收回应 
            
        }
        setInterval("polling()",2000); //1000毫秒 等于1秒
    </script>
</html>

然后再创建一个polling.php的文件

<?php
    $link = mysql_connect('localhost','root','') or die('数据库链接失败');
    mysql_query('set names utf8');
    mysql_select_db('xsphp');
    $randnumber = $_GET['randnumber'];
    $result = mysql_query("select * from user where randnumber='{$randnumber}'");
    $row = mysql_fetch_array($result);
    if($row['username'] != ''){
        echo "true";
    }else{
        echo 'false';
    }

然后再创建一个hello.php的文件用于跳转页面用的

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <h1>欢迎登录!!!!</h1>
    </body>
</html>

下一步就是saveusername.php的文件用于自定义PHPapi接口的

<?php
    /**
     *自定义api用于手机客户端扫码后访问,将指定的username保存至相应的位置
     *接收参数randnumber username
     *无返回值
    */
    
    $randnumber = $_GET['randnumber'];
    $username = $_GET['username'];
    
    //数据库链接操作
    $link = mysql_connect('localhost','root','') or die('数据库链接失败');
    mysql_query('set names utf8');
    mysql_select_db('xsphp');
    
    $sql = "update user set username='{$username}' where randnumber='{$randnumber}'";
    mysql_query($sql);
    

然后在浏览器上输入

http://www.showtp.com/saveusername.php?randnumber=20431715&username=leigood

然后再查看数据库即发现数据库已经成功录入

原文地址:https://www.cnblogs.com/leigood/p/5038693.html