Php generates an Android client to scan the login QR code _ php instance

Source: Internet
Author: User
This article describes in detail how php generates an Android client to scan the QR code that can be logged on. It has some reference value, interested friends can refer to the examples in this article to share with you the QR code generated on the php web page, Android Client Scan and login specific code for your reference, the specific content is as follows

The ZXing open-source library with code scanning function on Github is used, and the web API for generating QR code images through random numbers is used. The entire process goes through three steps:

1. The PHP web page generates a QR code and the random number is stored in the database;
2. Scan the Android client and save the username to the location corresponding to the random number;
3. at intervals of time, PHP poll the database through Ajax to determine whether it is empty. If it is not empty, it will jump to the webpage.

Code:
1. Generate a QR code image by using a random number, and execute the polling Operation Command on the home page.

   Qrlogin  
   <? Php/*** @ author Cenquanyu * @ version May 12, 2016 **/require 'mysql _ connect. php '; $ randnumber = ""; for ($ I = 0; $ I <8; $ I ++) {$ randnumber. = rand ();} // Save the generated random number to the database mysql_query ("insert into login_data (randnumber) values ('$ randnumber')")?> "Width =" 300px "/>  Script xmlHttpRequest. onreadystatechange = function () {if (xmlHttpRequest. status = 200 & xmlHttpRequest. readyState = 4) {result = xmlHttp. responseText; if (result = true) {// if username is not empty, the page window is redirected. location. href = 'Welcome. php ';}}} function polling () {// execute the polling operation 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

2. Database Connection page

<? Php/*** database connection file * @ author Cenquanyu * @ version July 22, May 12, 2016 **/$ con = mysql_connect ("localhost", "root ","") or die (mysql_error (); mysql_select_db ("qr_login");?>

3. For the page on which the round-robin operation is executed, jump if username is not empty

<? Php/*** @ author Cenquanyu * @ version May 12, 2016 * executes the round robin operation. If the username field in the corresponding location of the random number in the database is not blank *, false is returned, if the page is not redirected * not blank, it indicates that a user has scanned the QR code and logged on to the page */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. Customize the API to save the username of the Client

<? Php/*** @ author Cenquanyu * @ version May 12, 2016 custom API is used for Android client QR code logon. The username of the client is saved to the corresponding random number location of the QR code in the database. * Parameter: username, randnumber * No return value */$ randnumber = $ _ GET ('randnumber'); $ username =$ _ GET ('username '); require 'mysql _ connect. php '; mysql_query ("update qr_login set username =' $ username' where randnumber = '$ randnumber'");?>

5. Activity on the Android client performing the code scanning operation

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 May 12, 2016 **/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/ "; // Change to the corresponding address on the PC end @ 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) {// scan the code to operate Intent intent = new Intent (this, CaptureActivity. class); start ActivityForResult (intent, 0); // return result} @ 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"); // The client returns the scan result after scanning the code. The random number corresponding to the QR code is retrieved. String username = etUsername. getText (). toString (); String url = WEB_URL + "saveUsername. php? Randnumber = "+ randnumber +" & username = "+ username; HttpUtils. login (url); // access url }}}

6. Network request

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

The above is all the content of this article, hoping to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.