Android Scan webpage QR code for Web login

Source: Internet
Author: User

Reprint please indicate the source:
http://www.cnblogs.com/dingxiansen/;
This article originates from: Dinciensen-Blog Park

Saturday and friends to open the black bar, open TGP, the friend began to enter the account password, I saw his one eye low force, now who also manually input, mobile phone sweep a sweep on the good, and then open the mobile app sweep a scan login success, flattered. In fact, we are not difficult to find, a lot of web pages in order to easily login in addition to the third-party program login, is the QR code scan login, such as a treasure, a fish and so on, in fact, I think the QR code scan is not just for fast login, so that people save the trouble of entering account password, there is a to promote Have to say that this can also be used as a means to promote the app, good to get to the point.

First of all, the implementation of what effect, the app scanned PHP written pages (also can Java Web pages, the same ideas and steps), scan success after jumping into the homepage, light and you say so, certainly someone said, not a JB, this comes out a problem, a lot of people write blog, see this title particularly in line with their own problems, Then the cheerful came in, a look, I went to ... All is the code, do not know is not my kind of problem and want the kind of effect, heart flaky ah, good,:


Don't tangle this page so ugly, did not write the style, said I realized the idea ha, first in the database to save a new table to store the scan code login information (id,username,randomnum), the three is the ID from the growth, user name, random number--click to generate two-dimensional code, Generate random number, you can use http://qr.topscan.com/api.php?text= "+ Data, you can generate two-dimensional code, of course, you can also write your own, this operation is equivalent to a treasure of the two-dimensional code login box, after the generation of two-dimensional code, This time to do an AJAX request operation, in the newly created table to insert data, the generated random number is saved to the table, this time username to open the app scanning operation, after the app scan is completed, the network request, the QR code you scanned as parameters, Do the modify operation, modify username for your current user name--page use every few seconds to request another interface, according to the random number to determine whether the current username is empty, if not empty (just Sweep code modified username so not empty) jquery operation to do jump operation , this is the complete process.

The simple thing to say is that when you generate a QR code, only the random number (or your own custom) is used, and the user name is stored in a random number after the app is scanned, and when the Web page requests another interface every 5s, if the username is not empty, the jump operation is good. This is generally the case, it is possible that the realization of the great God is not so, I just say that I understand, if you have another way to achieve can also be in the comments below to tell me, study together, the following code.

Some PHP (novice may not be too standard, do not spray) login.php code:

Else if($_request[' act ']! = ' &&$_request[' act '] = = ' Codecreate ') {    //if username is empty---> represents the first time you click New Build//page generation QR code operation, add data to table    if($_request[' userName ']! = ') {//do not indicate changes when empty        $userName=$_request[' UserName ']; $randomNum=$_request[' Randomnum ']; Echo"Sweep Code Success"; $sql= "Update Usercodelogin set Username="$userName' WHERE randomnum= '$randomNum‘";//mysqli_query ($conn, $sql);//Insert Successful}Else{//when empty, indicates the new QR code//Generate random number        $randomNum= "";  for($i= 0;$i< 5;$i++) {            $randomNum.=Rand(0, 9); }        Echo $randomNum;//displays the generated random number        $sql= "INSERT into Usercodelogin (username,randomnum) VALUES (' ', '$randomNum‘)";//mysqli_query ($conn, $sql);//Insert Successful    }    Mysqli_query($conn,$sql);//Insert Successful} Else if($_request[' act ']! = ' &&$_request[' act '] = = ' Codeselect ') {//query whether the username in the table is empty based on the random number    $randomNum=$_request[' Randomnum ']; if($randomNum= = ' &&Empty($randomNum)) {    } Else if($randomNum! = ") {        $sql= "Select username from usercodelogin where randomnum= '$randomNum‘"; $result=Mysqli_query($conn,$sql);//Print_r ($result);        if(mysqli_num_rows($result) > 0) {             while($row=Mysqli_fetch_assoc($result)) {                $userName=$row[' username ']; }            if($userName= = ' &&$userName==NULL) {                //Description The client has not yet been scanned for code modified}Else if($userName! = ") {                //Client Sweep code completion//Jump page                Echo"Client sweep code success, jump in ..."; }        }    }

And then the login.html.

<HTML><Head>    <title>This is the homepage</title></Head><Scripttype= "Text/javascript"src= "Js/jquery-1.12.3.js"></Script><Body><Scripttype= "Text/javascript">    vartype= 1; varRandomnum; functionclickbtn () {$.post (".. /login.php?act=codecreate&username=",            function(data) {alert ("-------Random:" +data); Randomnum=data; $("#img"). attr ('src', "http://qr.topscan.com/api.php?text=" +data);        }); Type= 2; }    functionTime () {$.post (".. /login.php?act=codeselect&randomnum=" +Randomnum,function(data) {if(data) {alert (data); $ (location). attr ('href', 'index.html'); } Else{}})} setinterval ("Time ()", 4000);</Script><Buttontype= "Submit"ID= "BTN"onclick= "clickbtn ()">Click Generate QR Code</Button><!--src= "Https://www.baidu.com/img/baidu_jgylogo3.gif" -<imgID= "img"style= "Visibility:visible"width= "250px"/></Body></HTML>

Above is the service side of the code, rest assured that the source, the bottom of the end to write together

The following start Android on Android scan Code third party open source there is quite a lot of, here is zing, he can achieve a lot of QR code, barcode, generate two-dimensional code ...

Build class Library, guide jar package, drop, you can take this class library directly, and then change the style you need.

This diagram is the directory structure

The main thing is that mainactivity.java inside of the call

Sweep code after the successful return:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); //scan QR Code/barcode Callback        if(Requestcode = = Request_code_scan && ResultCode = =RESULT_OK) {            if(Data! =NULL) {String content=Data.getstringextra (constant.coded_content); Result.settext ("Scan results are:" +content); /*Perform network requests*/String username= "Dingchao";                Volleyscancodelogin (content, username); LOG.E ("AAA", "Volleyscancodelogin ... 3 "); }        }    }
Volleyscancodelogin () Method:
/*** Network request * Use volley for network requests * *@paramrandom number and user name * for content carrying@paramUsername User name*/    Private voidVolleyscancodelogin (FinalString content,FinalString username) {LOG.E ("AAA", "Volleyscancodelogin ... 1 "); FinalRequestqueue mqueue = Volley.newrequestqueue (mainactivity. This); Stringrequest stringrequest=NewStringrequest (Request.Method.POST, "http//Your own directory address/webcodelogin/login.php",NewResponse.listener<string>() {@Override Public voidOnresponse (String s) {LOG.E ("AAA", "Volleyscancodelogin ... 2 "); Toast.maketext (mainactivity. This, "" +s, Toast.length_short). Show (); }        }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {}}) {@OverrideprotectedMap<string, string> getparams ()throwsAuthfailureerror {Map<string, string> map =NewHashmap<string, string>(); Map.put ("Act", "Codecreate"); Map.put ("UserName", username); Map.put ("Randomnum", content); returnmap;        }        }; Mqueue.add (stringrequest);/*Request Data*/    }

Above those is the main engineering code, and then the source I packed into the Baidu cloud disk, Link: https://pan.baidu.com/s/1slsQM2D Password: GDYP

If there are shortcomings or deficiencies please feel free, if the integration has a problem, please leave a message, or email [email protected]

Android Scan webpage QR code for Web login

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.