Php is still very easy to implement the Sina Weibo synchronous login function. it is easy to add the login module to the php & amp; nbsp; sdk of Sina Weibo, the following describes how to implement synchronous login on Sina Weibo through php. The main principle is to grant the acces
It is still very easy to implement the Sina Weibo synchronous login function using php. it is easy to add the login module to the Sina Weibo php sdk. The following describes the process of implementing the Sina Weibo synchronous login through php, the main principle is to save the other information of the access_token owner in the local database after the Sina Weibo logon authorization, so as to bind the access_token, you only need to determine the existence of the access_token in the next user logon count, it indicates that the user has a database and the login is successful!
First, we need to analyze the following login situations:
1. the database has this user. log on directly! After determining the user and password, you need to consider binding to Sina Weibo after logging on.
2. the database has this user. log on to Sina Weibo! Determine whether the access_token field exists. if the field exists, the logon succeeds. if the field does not exist, the logon fails.
2. this user does not exist in the database. Sina Weibo login is invalid! Prompt this user to register
Implementation process:
First, create a database:(As shown below)
Note: there is a user table in the test Database. the access_token field records the access_token of Sina Weibo users!
My file directory format is:
Note: I put the php sdk package in the api folder. By the way, the above phpinfo. php does not exist and is incorrect!
The source code is as follows:
Login. php:
GetAuthorizeURL (WB_CALLBACK_URL);/* www.phpddt.com */if (! Empty ($ _ POST ['submit ']) {$ mysqli = new mysqli ('localhost', 'root', '', 'test '); $ username = $ _ POST ['username']; $ password = MD5 ($ _ POST ['password']); $ SQL = "select * from 'user' where 'username' = '$ username' and 'password' =' $ password '"; $ query = $ mysqli-> query ($ SQL); $ row = $ query-> fetch_array (); if ($ row [0]) {$ _ SESSION ['id'] = $ row ['id']; header ("Location: user. php ") ;}else {echo" incorrect user name or password ";}}?>
User. php:
Query ($ SQL); $ row = $ query-> fetch_array (); echo "login successful, welcome :". $ row ['username']; if ($ row ['Access _ token']) {echo "your Weibo account has been bound! ";} Else {echo" ==> bind Sina Weibo? Log on to the ";}} else {echo" logon failed. return to the logon page. ";}?>
Weibo login binding page: weibolist. php
Query ($ SQL); $ row = $ res-> fetch_array (); if (! Empty ($ row ['Access _ token']) {$ _ SESSION ['id'] = $ row ['id']; header ("Location :.. /user. php ");} else {if (! Empty ($ _ SESSION ['id']) {// bind $ SQL = "update 'user' set 'Access _ token' = '{$ _ SESSION ['token'] ['Access _ token']}' where 'id' = {$ _ SESSION ['id']} "; $ mysqli-> query ($ SQL); header ("Location :.. /user. php ") ;}else {echo" the user does not exist. return to the logon page to register ";}}$ res-> free (); $ mysqli-> close ();?>
The source code of other pages remains unchanged. for example, you must enter the app_key app_secrect and callback page callback addresses correctly in config. php in the api file!
As follows:
Source code download: (you need to configure the app_key app_secrect and callback page callback address !)
Export phpddt.com#sinaweibo.rar (I am a newbie, and my program will inevitably be wrong. please give me some advice !)