Realization method of automatic login for micro-scan Code website

Source: Internet
Author: User
Tags openid

Magento Micro-Scan code website automatic Login

The case modeled on the micro-letter joint landing approach, micro-letter joint Landing Introduction:

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang= Zh_cn

After viewing the authorization interface Call (Unionid), it is not difficult to find out the callback address, the user confirmed the landing PC can jump

Get Unionid method

Public Function wcallbackaction () {

$code = $_get[' code '];
$state = $_get[' state '];
$setting = include Config_path. ' setting.php ';
$appid = $setting [' Weixin '] [' AppID '];
$appsecret = $setting [' Weixin '] [' Appsecret '];

if (empty ($code)) $this->showmessage (' authorization failed ');
try{

$token _url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $appsecret. ' &code= '. $code. ' &grant_type=authorization_code ';

$token = Json_decode ($this->https_request ($token _url));

}catch (Exception $e)
{
Print_r ($e);
}

if (Isset ($token->errcode)) {
Echo ' Echo ' <br/>Exit
}

$access _token_url = ' https://api.weixin.qq.com/sns/oauth2/refresh_token?appid= '. $appid. ' &grant_type=refresh_token&refresh_token= '. $token->refresh_token;
Turn into an object
$access _token = Json_decode ($this->https_request ($access _token_url));
if (Isset ($access _token->errcode)) {
Echo ' Echo ' <br/>Exit
}
$user _info_url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token->access_token. ' &openid= '. $access _token->openid. ' &AMP;LANG=ZH_CN ';
Turn into an object
$user _info = Json_decode ($this->https_request ($user _info_url));
if (Isset ($user _info->errcode)) {
Echo ' Echo ' <br/>Exit
}
Print user Information
Echo ' <pre> ';
Print_r ($user _info);
Echo ' </pre> ';

Get Unionid

$uid = $user _info->unionid;

}

User action processing is divided into login again and the first time landing

$sql = "Select h_user_id from dtb_user_binded as T1 left join Dtb_user_weixin as T2 on t1.u_id=t2.id where t1.u_type= '".
User:: $arrUtype [' weixin_num_t ']. "' and t2.openid= ' $user _info->unionid '";
$h _user_id = Core_db::getone ($sql);
if (!empty ($h _user_id)) {//The Weixin number is logged in again

}{//the Weixin for the first time,

}

PHP micro-Scan Code PC-side Automatic login Registration


The interface scope is snsapi_userinfo, the micro-letter landing one is the Web page authorization landing, the other is the micro-letter joint landing

Web page Authorization Login: http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

Micro-letter joint landing: Https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN

First: The micro-letter link with a logo to generate a two-dimensional code

For example, the link is https://open.weixin.qq.com/connect/oauth2/authorize?appid= '. $appid. ' &redirect_uri= '. $url. ' &response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect ' We can make a fuss over the state, Because of the state, what kind of micro-trust you're going to return?

Can be used as an identifier for a server and a micro-segment


Public Function creatqraction () {

if ($_get[' app ']) {
$wtoken =$_cookie[' Wtoken '];
$postdata =$_session[' w_state '];
if ($wtoken) {
$postdata = $wtoken;
}
<span style= "color: #3366ff;" >include Config_path. ' phpqrcode/'. ' Phpqrcode.php ';</span>
$sh = $this-&gt;shar1 ();
$value = "Https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&amp;redirect_uri=http ://www.xxx.net/login/wcallback&amp;response_type=code&amp;scope=snsapi_userinfo&amp;state= ". $ PostData. " &amp;connect_redirect=1#wechat_redirect ";

$errorCorrectionLevel = "L";

$matrixPointSize = "5";

QRCode::p ng ($value, False, $errorCorrectionLevel, $matrixPointSize);
}

}

Two-dimensional code is generated at this time the state is the identity, Phpqrcode can be downloaded at the end of the article, so we set the callback address Http://www.xxx.net/login/wcallback

You can handle the data in the Wcallback method insert user generation session, jump landing, PC can set a few seconds Ajax request server, once acquired

State, that is, implementation of the adjustment, micro-trust in the browser can be closed after processing, micro-letter JS can be achieved


Document.addeventlistener (' Weixinjsbridgeready ', function Onbridgeready () {
Weixinjsbridge.call (' CloseWindow ');

}, False);

can also authorize the landing to jump to the micro-mail Service number attention page


Header ("location:weixin://profile/gh_a5e1959f9a4e");

Wcallback method to do processing landing


$code = $_get[' code '];
$state = $_get[' state '];
$setting = include Config_path. ' setting.php ';
$appid = $setting [' Weixin '] [' AppID '];
$appsecret = $setting [' Weixin '] [' Appsecret '];

if (empty ($code)) $this->showmessage (' authorization failed ');
try{

$token _url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $appsecret. ' &code= '. $code. ' &grant_type=authorization_code ';

$token = Json_decode ($this->https_request ($token _url));

}catch (Exception $e)
{
Print_r ($e);
}

if (Isset ($token->errcode)) {
Echo ' Echo ' <br/>Exit
}

$access _token_url = ' https://api.weixin.qq.com/sns/oauth2/refresh_token?appid= '. $appid. ' &grant_type=refresh_token&refresh_token= '. $token->refresh_token;
//Convert to Object
$access _token = Json_decode ($this->https_request ($access _token_url));
if (Isset ($access _ Token->errcode)) {
echo ' Echo ' <br/>

Exit;
}
$user _info_url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $access _token->access_token. ' &openid= '. $access _token->openid. ' &LANG=ZH_CN ';
//Convert to Object
$user _info = Json_decode ($this->https_request ($user _info_url));
If Isset ($user _info-> Errcode) {
echo ' Echo ' <br/> Exit;
}
//print user information
//echo ' <pre> ';
//Print_r ($user _info);
//Echo ' </pre> ';

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.