欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年05月13日 16:19:00   作者:岑泉鄅  
這篇文章主要為大家詳細(xì)介紹了php生成Android客戶端掃描可登錄的二維碼嗎,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了php網(wǎng)頁生成二維碼,Android客戶端掃描登錄的具體代碼,供大家參考,具體內(nèi)容如下

使用了Github上具有掃碼功能的ZXing開源庫,使用了通過隨機(jī)數(shù)生成二維碼圖片網(wǎng)絡(luò)API,整個過程經(jīng)過三步:

1.PHP網(wǎng)頁生成二維碼,相應(yīng)隨機(jī)數(shù)存儲到數(shù)據(jù)庫中;
2.Android客戶端掃碼,攜帶username保存至隨機(jī)數(shù)對應(yīng)的位置;
3.每隔一段時間,PHP通過Ajax輪詢數(shù)據(jù)庫,判斷是否為空,不為空則跳轉(zhuǎn)網(wǎng)頁。

具體代碼:
1. 通過隨機(jī)數(shù)生成二維碼圖片,并執(zhí)行輪詢操作命令的主頁面    

<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ù)庫
    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)頁面
     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ù)庫連接頁面    

<?php
/**
 * 數(shù)據(jù)庫連接文件
 * @author Cenquanyu
 * @version 2016年5月12日
 * 
 */
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("qr_login");
 
?>

3.執(zhí)行輪詢操作的頁面,username不為空則跳轉(zhuǎn)    

<?php
/**
 * @author Cenquanyu
 * @version 2016年5月12日
 * 執(zhí)行輪詢操作,查詢隨機(jī)數(shù)在數(shù)據(jù)庫中的相應(yīng)位置的username字段為不為空
 * 為空,則返回false,頁面不跳轉(zhuǎn)
 * 不為空,則說明有用戶進(jìn)行了該二維碼的掃碼登錄,頁面進(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,對客戶端的username進(jìn)行保存    

<?php
/**
 * @author Cenquanyu
 * @version 2016年5月12日
 * 自定義API用于Android客戶端掃碼登錄,將客戶端的username保存至二維碼對應(yīng)的隨機(jī)數(shù)在數(shù)據(jù)庫中的相應(yīng)位置。
 * 參數(shù):username,randnumber
 * 無返回值
 */
$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é)果,將二維碼對應(yīng)的隨機(jī)數(shù)取出
      String username = etUsername.getText().toString();
      String url = WEB_URL + "saveUsername.php?randnumber=" + randnumber
          + "&username=" + username;
      HttpUtils.login(url);//訪問url
    }
  }
 
}

6. 網(wǎng)絡(luò)請求類

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();
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • PHP使用curl制作簡易百度搜索

    PHP使用curl制作簡易百度搜索

    這篇文章主要為大家詳細(xì)介紹了PHP使用curl制作簡易百度搜索的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例

    TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作示例

    這篇文章主要介紹了TP5(thinkPHP5)框架mongodb擴(kuò)展安裝及特殊操作,結(jié)合實例形式分析了MongoDB擴(kuò)展的基本安裝、配置、模型操作以及使用Push操作實現(xiàn)的數(shù)據(jù)添加、更新等方法,需要的朋友可以參考下
    2018-09-09
  • php日歷制作代碼分享

    php日歷制作代碼分享

    這篇文章主要介紹了使用php制作的日歷,下面有效果圖,大家參考使用吧
    2014-01-01
  • 一個比較不錯的PHP日歷類分享

    一個比較不錯的PHP日歷類分享

    這篇文章主要介紹了一個比較不錯的PHP日歷類分享,本文直接給出了實現(xiàn)的類代碼和使用方法示例,需要的朋友可以參考下
    2014-11-11
  • php數(shù)組去重復(fù)數(shù)據(jù)示例

    php數(shù)組去重復(fù)數(shù)據(jù)示例

    這篇文章主要介紹了php數(shù)組去重復(fù)數(shù)據(jù)示例,有時候獲得的php數(shù)組中總是出現(xiàn)value重復(fù)的,使用下面的方法就可以去掉重復(fù)數(shù)據(jù)
    2014-02-02
  • 深入理解PHP+Mysql分布式事務(wù)與解決方案

    深入理解PHP+Mysql分布式事務(wù)與解決方案

    這篇文章主要介紹了深入理解PHP+Mysql分布式事務(wù)與解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • PHP二進(jìn)制與字符串之間的相互轉(zhuǎn)換教程

    PHP二進(jìn)制與字符串之間的相互轉(zhuǎn)換教程

    歡迎大家在這里學(xué)習(xí)PHP二進(jìn)制與字符串之間的相互轉(zhuǎn)換!問題也肯定是很多朋友在關(guān)心的,想要了解的朋友可以看一下。
    2016-10-10
  • Yii CFileCache 獲取不到值的原因分析

    Yii CFileCache 獲取不到值的原因分析

    這篇文章主要介紹了Yii CFileCache 獲取不到值的原因分析,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2017-02-02
  • php格式文件打開的四種方法

    php格式文件打開的四種方法

    這篇文章主要介紹了php格式文件打開的四種方法,需要的朋友可以參考下
    2018-02-02
  • ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法分析

    ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法分析

    這篇文章主要介紹了ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法,結(jié)合實例形式分析了thinkPHP5.0數(shù)據(jù)庫的配置與動態(tài)連接相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10

最新評論