使用了Github上具有扫码功能的ZXing开源库,使用了通过随机数生成二维码图片网络API,整个过程经过三步:
1.PHP网页生成二维码,相应随机数存储到数据库中;
2.Android客户端扫码,携带username保存至随机数对应的位置;
3.每隔一段时间,php通过ajax轮询数据库,判断是否为空,不为空则跳转网页。
<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;? alt="PHP网页生成二维码,Android客户端扫码登录" >" 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>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号