php生成Android客戶端掃描可登錄的二維碼
本文實(shí)例為大家分享了php網(wǎng)頁(yè)生成二維碼,Android客戶端掃描登錄的具體代碼,供大家參考,具體內(nèi)容如下
使用了Github上具有掃碼功能的ZXing開(kāi)源庫(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ò)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>
<?php
/**
* @author Cenquanyu
* @version 2016年5月12日
*
*/
require 'mysql_connect.php';
$randnumber = "";
for($i=0;$i<8;$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è)面
<?php
/**
* 數(shù)據(jù)庫(kù)連接文件
* @author Cenquanyu
* @version 2016年5月12日
*
*/
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("qr_login");
?>
3.執(zhí)行輪詢操作的頁(yè)面,username不為空則跳轉(zhuǎn)
<?php
/**
* @author Cenquanyu
* @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";
else
echo "false";
?>
4.自定義的API,對(duì)客戶端的username進(jìn)行保存
<?php
/**
* @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'");
?>
5. Android客戶端執(zhí)行掃碼操作的Activity
package com.Cenquanyu.qrlogin;
import com.Cenquanyu.qrlogin.R;
import com.zxing.activity.CaptureActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint.Cap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* @author Cenquanyu
* @version 2016年5月12日
*
*/
public class MainActivity extends Activity implements OnClickListener {
private Button btnScan;
private EditText etUsername;
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)求類(lèi)
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í)有所幫助。
- Android App端與PHP Web端的簡(jiǎn)單數(shù)據(jù)交互實(shí)現(xiàn)示例
- Android md5加密與php md5加密一致詳解
- Android上傳文件到Web服務(wù)器 PHP接收文件
- Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能
- PHP簡(jiǎn)單判斷iPhone、iPad、Android及PC設(shè)備的方法
- php、java、android、ios通用的3des方法(推薦)
- 基于PHP后臺(tái)的Android新聞瀏覽客戶端
- Android異步上傳圖片到PHP服務(wù)器
- 使用PHP開(kāi)發(fā)Android應(yīng)用程序技術(shù)介紹
- Android訪問(wèn)php取回json數(shù)據(jù)實(shí)例
- android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
- Android和PHP MYSQL交互開(kāi)發(fā)實(shí)例
相關(guān)文章
PHP之認(rèn)識(shí)(二)關(guān)于Traits的用法詳解
這篇文章主要介紹了PHP Traits的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解
這篇文章主要介紹了mysql查找刪除重復(fù)數(shù)據(jù)并只保留一條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09
laravel-admin表單提交隱藏一些數(shù)據(jù),回調(diào)時(shí)獲取數(shù)據(jù)的方法
今天小編就為大家分享一篇laravel-admin表單提交隱藏一些數(shù)據(jù),回調(diào)時(shí)獲取數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁(yè)及相關(guān)函數(shù)詳解
ajax其實(shí)是利用javascript向服務(wù)器請(qǐng)求數(shù)據(jù),然后局部修改頁(yè)面,下面這篇文章主要給大家介紹了關(guān)于PHP實(shí)現(xiàn)AJAX動(dòng)態(tài)網(wǎng)頁(yè)及相關(guān)函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
PHP中關(guān)于PDO數(shù)據(jù)訪問(wèn)抽象層的功能操作實(shí)例
下面小編就為大家?guī)?lái)一篇PHP中關(guān)于PDO數(shù)據(jù)訪問(wèn)抽象層的功能操作實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
laravel框架關(guān)于搜索功能的實(shí)現(xiàn)
本文是作者整理的關(guān)于laravel框架搜索功能的實(shí)現(xiàn)原理,并附上了詳細(xì)代碼,有需要的小伙伴請(qǐng)持續(xù)關(guān)注!2018-03-03
PHP 實(shí)現(xiàn)數(shù)組分頁(yè)
在日常開(kāi)發(fā)的業(yè)務(wù)環(huán)境中,我們一般都會(huì)使用MySQL語(yǔ)句來(lái)實(shí)現(xiàn)分頁(yè)的功能。但是,往往也有些數(shù)據(jù)并不多,或者只是獲取 PHP 中定義的一些數(shù)組數(shù)據(jù)時(shí)需要分頁(yè)的功能。這時(shí),我們可以在一次查詢中把所有的數(shù)據(jù)取出來(lái),然后在 PHP 的代碼層面進(jìn)行分頁(yè)功能的實(shí)現(xiàn)2021-06-06

