本文实例为大家分享了php网页生成二维码,Android客户端扫描登录的具体代码,供大家参考,具体内容如下
使用了Github上具有扫码功能的ZXing开源库,使用了通过随机数生成二维码图片网络API,整个过程经过三步:
1.PHP网页生成二维码,相应随机数存储到数据库中;
2.Android客户端扫码,携带username保存至随机数对应的位置;
3.每隔一段时间,PHP通过Ajax轮询数据库,判断是否为空,不为空则跳转网页。
具体代码:
1. 通过随机数生成二维码图片,并执行轮询操作命令的主页面
<html> <head> <title>qrlogin</title> <meta charset="UTF-8"/> </head> <body> <?php /** * @author Cenquanyu * @version 2016年5月12日 * */ require 'mysql_connect.php'; $randnumber = ""; for($i=0;$i<8;$i++){ $randnumber.=rand(0,9); } //将生成的随机数保存至数据库 mysql_query("insert into login_data (randnumber) values ('$randnumber')") ?> <img src="http://qr.liantu.com/api.php?text=<?php echo $randnumber;?>" width="300px"/> <input hidden="hidden" type="text" name="randnumber" id="randnumber"value="<?php echo $randnumber;?>"/> </body> <script> xmlHttpRequest.onreadystatechange = function(){ if(xmlHttpRequest.status == 200 && xmlHttpRequest.readyState ==4){ result = xmlHttp.responseText; if(result==true){//username不为空则跳转页面 window.location.href='welcome.php'; }}} } function polling(){ //执行轮询操作 var xmlHttpRequest; if(window.XMLHttpRequest){ xmlHttpRequest = new XMLHttpRequest(); } else{ xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } randnumber = document.getElementById('randnumber').value; xmlHttpRequest.open("GET","polling.php?randnumber="+ randnumber,true); xmlHttpRequest.send(); } setInterval("polling()",1000);</script> </html>
2. 数据库连接页面
<?php/** * 数据库连接文件 * @author Cenquanyu * @version 2016年5月12日 * */$con = mysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("qr_login"); ?>
3.执行轮询操作的页面,username不为空则跳转
<?php/** * @author Cenquanyu * @version 2016年5月12日 * 执行轮询操作,查询随机数在数据库中的相应位置的username字段为不为空 * 为空,则返回false,页面不跳转 * 不为空,则说明有用户进行了该二维码的扫码登录,页面进行跳转 */require 'mysql_connect.php';$randnumber = $_GET['randnumber'];$result = mysql_query("select * from login_data where randnumber='$randnumber'");$row = mysql_fetch_array($result);if($row['username']!="") echo "true";else echo "false";?>
新闻热点
疑难解答