The Android +PHP+Swoole+Websocket client scans the TWO-DIMENSIONAL code on the web side to achieve login

Making portal


It mainly involves technology and class libraries

  1. PHP PHP qrCode class library generates a TWO-DIMENSIONAL code
  2. PHP swoole extension
  3. Redis PHP extensions
  4. Js Websocket client
  5. Android Zxing class library scan the QR code
  6. Android OKHTTP Web class library
  7. Mysql stores data

Note: the final implementation is DEMO version, do not take the production environment to use.

File List

  1. QRScanner. Apk Android scan qr code client (xiaomi 2S on the test, ha ha other do not know compatible, after all, not write Android)
  2. Api.php Android requests interface processing and forwards UDP to API_server.php
  3. Api_server.php server processing
  4. Qrlogin. SQL database configuration file

Basic configuration

1. Database connection configuration./db/ pdomysQL.clss

Private static $config = array(' dbType '=> 'mysql', 'dbhost' => '127.0.0.1', 'dbuser' => 'root', 'dbpass' => '123456', 'databases' => 'demo', 'dbport' => '3306', 'charset' => 'UTF8', 'pconnect' => false, 'debug' => true, ); Database according to their own environment configuration, this garbage code, we willCopy the code

File permissions./temp Read/write permissions (Linux)

3. Modify the token address configuration in index. PHP

``` $tokenURL = 'http://your_ip/qrlogin/api.php? token='.$uuid; ` ` `Copy the code

4. Configure api_server. PHP swoole_websocket_server, Redis, and UDP to listen to IP addresses and ports

$serv = new swoole_websocket_server("0.0.0.0", 9502); $redis = new Redis(); $redis - > connect (127.0.0.1, 6379); $serv-> addListener ('0.0.0.0', 9503, SWOOLE_SOCK_UDP); Make changes according to your actual environmentCopy the code

Basic implementation principles (no technical content)

  1. Browse the web (index.php), generate a Token(UUID can also be used), save it to MySql database or cache, there is Redis, websocket connects to Swoole, wait for push information

  2. The client scans the Token in the QR code. The Android client (user login status) requests api.php with the user name and Token or other identifiers to perform verification and update the database. Api.php sends a UDP request to api_server.php

  3. After receiving the UDP request, api_server.php pushes the login information to the web client

Wrote last

The implementation principle is simple, there are security problems, the realization of a way of thinking, not in front of a judge to teach fish to swim, learning is endless, so write code to go