国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

php生成Android客戶端掃描可登錄的二維碼

2020-03-22 17:50:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本文實(shí)例為大家分享了php網(wǎng)頁(yè)生成二維碼,Android客戶端掃描登錄的具體代碼,供大家參考,具體內(nèi)容如下使用了Github上具有掃碼功能的ZXing開源庫(kù),使用了通過(guò)隨機(jī)數(shù)生成二維碼圖片網(wǎng)絡(luò)API,整個(gè)過(guò)程經(jīng)過(guò)三步:
1.PHP網(wǎng)頁(yè)生成二維碼,相應(yīng)隨機(jī)數(shù)存儲(chǔ)到數(shù)據(jù)庫(kù)中;
2.Android客戶端掃碼,攜帶username保存至隨機(jī)數(shù)對(duì)應(yīng)的位置;
3.每隔一段時(shí)間,PHP通過(guò)html' target='_blank'>Ajax輪詢數(shù)據(jù)庫(kù),判斷是否為空,不為空則跳轉(zhuǎn)網(wǎng)頁(yè)。
具體代碼:
1. 通過(guò)隨機(jī)數(shù)生成二維碼圖片,并執(zhí)行輪詢操作命令的主頁(yè)面
html head title qrlogin /title meta charset="UTF-8"/ /head body * @author Cenquanyu * @version 2016年5月12日 require 'mysql_connect.php'; $randnumber = ""; for($i=0;$i $i++){ $randnumber.=rand(0,9); //將生成的隨機(jī)數(shù)保存至數(shù)據(jù)庫(kù) 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不為空則跳轉(zhuǎn)頁(yè)面 window.location.href='welcome.php'; function polling(){ //執(zhí)行輪詢操作 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. 數(shù)據(jù)庫(kù)連接頁(yè)面
* 數(shù)據(jù)庫(kù)連接文件 * @author Cenquanyu * @version 2016年5月12日$con = mysql_connect("localhost","root","") or die(mysql_error());mysql_select_db("qr_login");
* @version 2016年5月12日 * 執(zhí)行輪詢操作,查詢隨機(jī)數(shù)在數(shù)據(jù)庫(kù)中的相應(yīng)位置的username字段為不為空 * 為空,則返回false,頁(yè)面不跳轉(zhuǎn) * 不為空,則說(shuō)明有用戶進(jìn)行了該二維碼的掃碼登錄,頁(yè)面進(jìn)行跳轉(zhuǎn)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"; echo "false";4.自定義的API,對(duì)客戶端的username進(jìn)行保存
* @author Cenquanyu * @version 2016年5月12日 * 自定義API用于Android客戶端掃碼登錄,將客戶端的username保存至二維碼對(duì)應(yīng)的隨機(jī)數(shù)在數(shù)據(jù)庫(kù)中的相應(yīng)位置。 * 參數(shù):username,randnumber * 無(wú)返回值$randnumber = $_GET('randnumber');$username = $_GET('username');require 'mysql_connect.php';mysql_query("update qr_login set username='$username' where randnumber= '$randnumber'");
private static final String WEB_URL = "http://172.31.19.202/QRLogin/";//改成PC端相應(yīng)地址 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnScan = (Button) findViewById(R.id.btnScan); btnScan.setOnClickListener(this); etUsername = (EditText) findViewById(R.id.etUsername); @Override public void onClick(View v) { // 掃碼操作 Intent intent = new Intent(this, CaptureActivity.class); startActivityForResult(intent, 0);//返回結(jié)果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { String randnumber = data.getExtras().getString("result");//客戶端掃碼后返回掃描結(jié)果,將二維碼對(duì)應(yīng)的隨機(jī)數(shù)取出 String username = etUsername.getText().toString(); String url = WEB_URL + "saveUsername.php randnumber=" + randnumber + "&username=" + username; HttpUtils.login(url);//訪問(wèn)url}6. 網(wǎng)絡(luò)請(qǐng)求類
package com.Cenquanyu.qrlogin;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class HttpUtils{ public static void login(final String url){ new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection; try { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("GET"); connection.getInputStream(); } catch (Exception e) { e.printStackTrace(); }).start();以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 竹山县| 孟村| 汉中市| 漳州市| 谢通门县| 本溪市| 城口县| 定结县| 云浮市| 伊金霍洛旗| 神木县| 饶阳县| 利津县| 盐边县| 天峻县| 鹤岗市| 若羌县| 辛集市| 海林市| 招远市| 克东县| 衡水市| 花垣县| 竹山县| 霍城县| 福泉市| 开江县| 武汉市| 东港市| 老河口市| 永康市| 洱源县| 泽普县| 株洲县| 万宁市| 岑巩县| 常熟市| 奈曼旗| 同心县| 万年县| 红桥区|