Requirement introduction
First of all, let’s introduce what scanning code login is. Now, most students have QQ, Taobao, Tmall and other software installed on their mobile phones. The companies that develop these apps have their corresponding websites. In order to make login more convenient and secure for users when using their website. These companies provide services where you can log in using your mobile phone by scanning. The effect when logging in to the web page is as follows:


Principle explanation
Web page server
The next step is the detailed implementation of this service. When a user opens the website login page, a request will be sent to the browser server to obtain the login QR code. This is the principle.
After the server receives the request, it randomly generates a uuid, stores this id as a key value in the redis server, and sets an expiration time. After expiration, the user's login QR code needs to be refreshed and reacquired. At the same time, combine this key value with the company's verification string, and generate a QR code image through the QR code generation interface (QR code generation, there are many ready-made interfaces and source codes on the Internet, which will not be introduced here. .) Then, return the QR code image and uuid to the user's browser.
The browser will periodically send requests to the server to check whether the login is successful. These requests include QR code and UUID information. The request carries uuid as the identifier of the current page. Some students may be confused because the server only stores UUID as a key in Redis, so where does the user's ID information come from?
There will indeed be user ID information here. This ID information is stored in redis by the mobile server.
Mobile Server
When the browser obtains the QR code, it displays it on the web page and prompts the user: "Please take out your mobile phone and open Scan to log in ". The user takes out his mobile phone and scans the QR code to get a verification information and a uuid (the function of scanning the QR code to obtain a string also has many demos on the Internet, so I will not introduce it in detail here).
Since the mobile terminal has already logged in, when accessing the mobile terminal server, the parameters will carry a user's token, and the mobile terminal server can parse it to the user's userId (here the value is obtained from the token) Instead of transmitting the userid directly on the mobile phone, it is for security. Directly transmitting the userid may be intercepted and modified. The token is encrypted, so the risk of modification will be much smaller).
The mobile phone uses the parsed data and the user token as parameters to send a verification login request to the server (the server here is the mobile server, and the mobile phone server is not the same server as the web server). After receiving the request, the server will first compare the verification information in the parameters to confirm whether it is the interface requested by the user to log in. If so, return a confirmation message to the mobile phone.
When the mobile phone receives a response, the login confirmation box should be displayed to avoid user misoperation and improve the login experience. After the user confirms that the login operation is carried out, the mobile phone sends the request again. After the server gets the uuId and userId, it stores the user's userid as the value in a key-value pair in redis with uuid as the key.
Login successful
Then, when the browser sends a request again, the browser-side server can get a user ID, call the login method, and sound it into a browser-side token, and then When the browser sends the request again, the user information is returned to the browser and the login is successful. We choose to store user IDs rather than store user information directly because the user information on the mobile phone and the browser may not be exactly the same.
The login schematic diagram is as follows:

The above is the detailed content of How to implement code scanning login in redis. For more information, please follow other related articles on the PHP Chinese website!

RedisofferssuperiorspeedfordataoperationsbutrequiressignificantRAMandinvolvestrade-offsindatapersistenceandscalability.1)Itsin-memorynatureprovidesultra-fastread/writeoperations,idealforreal-timeapplications.2)However,largedatasetsmaynecessitatedatae

Redisoutperformstraditionaldatabasesinspeedforread/writeoperationsduetoitsin-memorynature,whiletraditionaldatabasesexcelincomplexqueriesanddataintegrity.1)Redisisidealforreal-timeanalyticsandcaching,offeringphenomenalperformance.2)Traditionaldatabase

UseRedisinsteadofatraditionaldatabasewhenyourapplicationrequiresspeedandreal-timedataprocessing,suchasforcaching,sessionmanagement,orreal-timeanalytics.Redisexcelsin:1)Caching,reducingloadonprimarydatabases;2)Sessionmanagement,simplifyingdatahandling

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
