PHP generates Android client scans _php instance of two-dimensional code that can be logged in

Source: Internet
Author: User

This example for you to share the PHP Web generated two-dimensional code, Android client scan login specific code for your reference, the specific content as follows

Using the Zxing open Source Library with sweep code on the GitHub, the network API is used to generate two-dimensional code picture via random number, the whole process is three steps:

1.PHP Web page to generate two-dimensional code, the corresponding random number stored in the database;
2.Android Client sweep code, carry username save to the location of random number corresponding;
3. Every once in a while, PHP polling the database through Ajax, judge whether it is empty, not NULL to jump the page.

Specific code:
1. Generate two-dimensional code picture by random number and execute the main page of polling operation command

 

2. Database Connection page

<?php
/** *
 database Connection File
 * @author cenquanyu
 * @version May 12, 2016 * */
$con = Mysql_ Connect ("localhost", "root", "") or Die (Mysql_error ());
mysql_select_db ("Qr_login");
 
? >

3. The page that executes polling action, username is not empty then jumps

<?php
/**
 * @author cenquanyu *
 @version May 12, 2016
 * Polling operation, query random number username field in the corresponding location in the database is not empty
 * is empty, then return false, the page does not jump
 * is not empty, then the user has carried out the two-dimensional code to scan the code login, the page jumps
/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. Custom API to save the username of the client

<?php
/**
 * @author cenquanyu *
 @version May 12, 2016
 * Custom API for Android client scan code login, Save the client's username to the corresponding location in the database for the random number corresponding to the two-dimensional code.
 * 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. The activity that the Android client performs the sweep code 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 Onclickliste
  NER {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 side @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) {//Sweep code operation Intent Intent = new Intent (this, captureactivity.class); Startactivityforresult (Intent, 0);//Return result} @Override protected void Onactivityresult (int requestcode, int Resultco
    DE, Intent data) {Super.onactivityresult (Requestcode, ResultCode, data); if (ResultCode = = ACTIVITY.RESULT_OK) {String randnumber = Data.getextras (). getString ("result");//The client sweeps the code and returns the scan, the two-dimensional code
      corresponding random number remove String username = Etusername.gettext (). toString ();
      String url = web_url + "saveusername.php?randnumber=" + randnumber + "&username=" + username; Httputils.login (URL);//Access URL}}}

6. Network Request Class

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 c10/>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 the entire content of this article, I hope to help you learn.

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.