Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of scan by php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces php how to achieve scan function, the article introduces in great detail, has a certain reference value, interested friends must read it!

The method of php to realize scan function is as follows: 1, generate a QR code, generate a unique ID uuid, and use redis to save temporary information; 2, maintain the communication between the user request and the server through a long connection; 3, use the mobile phone to scan the QR code to obtain the unique ID uuid, and judge whether the ID is valid.

This article operating environment: Windows7 system, PHP7.4 version, Dell G3 computer.

Log in by scanning PHP code

The specific implementation method of scanning code login

The specific implementation process of scanning code login:

1. Generate a QR code and a unique identity uuid, which runs through the whole process. Use redis to save temporary information.

(uuid is used as key, and the corresponding value set to-1 indicates that it has not been scanned. The validity period of the logo is set to three minutes here, and a QR code will be generated when it expires.)

Public function index () {$uuid = self::createRandomStr ('6'); $url = $this- > code ($uuid); / / QR code link address Redis::sAdd ($uuid,'-1'); Redis::command ('Expire', [$uuid,180]); / / set expiration time return view (' home.send.index', ['url'= > $url,'uuid'= > $uuid]) } / * generate the QR code url * @ param $text * / public function code ($uuid) {$host=' http://'.$_SERVER["HTTP_HOST"].'/api/send'; $url=' http://qr.liantu.com/api.php?text='.$host.'?uuid='.$uuid; return $url 2. The login page maintains the communication between the user request and the server through a long connection, and monitors whether the corresponding value of uuid in the redis changes at regular intervals.

(- 1 is not scanned, 0 indicates that the user has scanned the QR code ps: when the QR code is scanned again, the prompt is invalid, and the remaining values indicate the user's primary key id)

/ * verify whether the uuid is out of date * @ param Request $req * @ return string | void * / public function confirm (Request $req) {$uuid = $req- > uuid; $result = implode (', Redis::sort ($uuid)); if ($result = =') {/ / if the uuid does not exist {/ / the QR code return 'the link address has expired' is generated. } if ($result = ='- 1') {return';} elseif ($result = ='0') {return 'user scanned';} else {return 'user confirmed login'; / / get the user's user_id login directly. }} 3. Use the mobile phone to scan the QR code (ps: the self-developed App should be used here, and the App has been logged in) to obtain the unique ID uuid to determine whether the logo is valid (if the logo does not expire, modify the value of uuid to 0 to indicate that the QR code has been scanned). Public function index (Request $req) {$uuid = $req- > uuid; $info = Code::getOne (['uuid'= > $uuid]); $result = implode (', Redis::sort ($uuid)); if (! $result) {return $this- > ajaxMsgError ('the QR code is invalid');} Redis::sRem ($code,'-1') Redis::sadd ($code, 0); return $this- > ajaxMsgOk ('scan successful, please confirm login');} 4. APP confirm login and change the vale value corresponding to the current uuid to the primary key idpublic function dologin (Request $req) {$user_id = $req- > user_id; $uuid= $req- > uuid; $user = User::getOne (['id'= > $user_id]) of the currently logged-in user. If ($user) {if ($user- > status! = 1) {return $this- > ajaxMsgError ('this user has been disabled');} Redis::sadd ($uuid,$user_id); return $this- > ajaxSuccess ('login successful') } else {/ / user does not exist return $this- > ajaxError ('this user does not exist');}} 5. Repeat step 2. If this API checks that the key value in redis is the login person UID, it will return the login information and save the login status. The above is all the contents of the article "how to scan php". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report