PHP How to write a client scan QR code login PC Side Backend function

Source: Internet
Author: User
Tags php write unique id

This article is mainly to share with you how PHP write client scan qr code login PC backend function, I am also the first time to write this function, under Bo main blog inspired by the demo machine to complete this function, I want to re-summarize this code, in case forget.

HTML page

<! DOCTYPE html>

JS Page

$.ajax ({type: "GET", url: "er/qrcodelogin/getqruuid.php", data: {}, Success:                function (Result) {Console.log (result);                var data = Json.parse (result);                Console.log (Data.code);                    if (Data.code = = 1) {$ ("#qrcodeimg"). attr (' src ', ' http://qr.liantu.com/api.php?text= ' + result);                    Console.log (Data.data);                    var min=0;                        Interval1= setinterval (function () {min=min+1000;                            $.ajax ({type: "GET", url: "er/qrcodelogin/checkqruuid.php",                                    Data: {' Qruuid ': Data.data}, Success:function (result) {                                    min=min+1000;                              if (min==36000) {console.log (min);          Alert ("The QR code has expired!")                                        ");                                        Clearinterval (INTERVAL1);                                        Location.replace (LOCATION.HREF);                                        min=1000;                                    Interval1 ();                                        }else{console.log (min);                                        var data = Json.parse (result);                                            if (Data.code = = 1) {//alert (' Scan successful (ie login succeeded), jump ...... ');                                            Stop polling clearinterval (INTERVAL1);                                            Location.href= "index.html";                                            TODO get the information needed and then jump to what's}else if (Data.code = = 300) { Alert (' No subsequent user!                                           '); Stop polling clearinterval (INTERVAL1);                                        Location.href= "login.html";                    }                                    }                            }                        }); }, 1000);//1 Second frequency On Demand}});

getqruuid.php page

<?php/** * For front end get Qruuid (QR code unique ID) using * * Created by Phpstorm. * User:caohan * DATE:2017/10/15 * Time: PM 2:59 */require (' config.php ');//Generate random UUID for the contents and bindings of the QR code display with $QRUUID = substr (MD5 ( Uniqid (Mt_rand (), true)), 0, 15),//Generate uuid//Save the generated random number to the database $sql= INSERT into T_pc_code (randnumber) VALUES (' $qruuid '); query = sqlsrv_query ($conn, $sql), if (! $query) {die       (Print_r (Sqlsrv_errors (), True));} $arr = [' Code ' =>1, ' msg ' = ' = ' Generate Qruuid success ', ' data ' = $qruuid];echo json_encode ($arr); exit ();

checkqruuid.php page

<?phpsession_start ();/** * Created by Phpstorm. * User:caohan * DATE:2017/10/15 * Time: PM 3:17 */require (' config.php '); $mysqli = new Mysqli ($db _host, $db _user, $db _pwd, $db _name); if (Mysqli_connect_error ())//Echo Mysqli_connect_error ();  $mysqli->set_charset ("UTF8"); $qruuid = $_get[' Qruuid '); $sql = "SELECT * from T_pc_code where randnumber= '". $qruuid. "'"; $result = Sqlsrv_fetch_array (sqlsrv_query ($conn, $sql)), if (! $result) {die (Print_r (Sqlsrv_errors (), True));}    if (!is_null ($result [' username '])) {$nowusername = $result [' username '];    $sql 2= "SELECT * from T_webuser where username = ' $nowusername ' and isdeleted=0 ';    $result 2 = sqlsrv_query ($conn, $sql 2);         if ($row =sqlsrv_fetch_array ($result 2)) {$_session[' sydsdj_name ']= $row [' username '];         $_session[' Sydsdj_roletype ']= $row [' Roletype '];          $_session[' Sydsdj_orgid ']= $row [' orgId '];          $_session[' sydsdj_orgname ']= $row [' OrgName ']; $_session[' sydsdj_manage_id ']= $row [' ID'];    $arr = [' Code ' = + 1, ' msg ' = ' login successful ', ' data ' = ' $result]; }else{$arr = [' Code ' = ' + ' msg ' = ' Account not registered!    ', ' data ' = $result]; }}else{$arr = [' Code ' = ', ' msg ' = ' qruuid ' is temporarily unbound ', ' data ' = ' $qruuid];} echo Json_encode ($arr); exit ();? >

Interface to client storage user accounts

<?php//specifies that other domain names are allowed to access the  header (' access-control-allow-origin:* '); require (' config.php '); $username = $_request[' Username ']; $randnumber = $_request[' Randnumber '); $result = sqlsrv_query ($conn, "UPDATE T_pc_code set Username= ' $ Username ' where randnumber= ' $randnumber '), if (! $result) {die    (Print_r (Sqlsrv_errors (), true));    $arr = [' Code ' = + 0, ' msg ' = ' sy '];} else{    $arr = [' Code ' = = 1, ' msg ' = ' sy '];} echo Json_encode ($arr);? >

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.