How to handle page authorization on WeChat public platform (single method)

Source: Internet
Author: User
Tags oauth openid
Provides various official and user-released code examples and code reference. You are welcome to exchange and learn how to use the public platform for third-party development, we often encounter the need to call page authorization to obtain user information. However, when you use page authorization to access a page, the page is always redirected to the page for authorization. This increases the time required for users to access the page and affects the user experience.
Therefore, we need a method, such as using other mechanisms, which does not require users to jump to the page authorization location every time. In this way, when a user does not enter the page for the first time, he or she does not need to jump to the page for authorization, which reduces the page entry time and improves the user experience.
In this way, the server session mechanism can be used to determine and use if.
The sample code is as follows: Public function OAuth (){
Header ('content-type: text/html; charset = UTF-8 ');
// Obtain the configuration information first
$ M_sssmall_conf_weixin = M ('sssmall _ conf_weixin ');
$ Conf = $ m_sssmall_conf_weixin-> where ('Id = 1')-> find ();
// Set appid and appsecret as super global variables for easy calling.
$ Appid = $ conf ['appid '];
$ Appsecret = $ conf ['appsecret'];
// $ Access_token = getApiToken ();
Dump ($ _ SESSION ['oauth ']);
// Condition determination (if the session does not exist, perform page authorization and set the session)
If (empty ($ _ SESSION ['oauth']) & empty ($ _ GET ['code']) {
$ Redirect_uri = $ _ SERVER ['request _ SCHEME ']. '://'. $ _ SERVER ['HTTP _ host']. $ _ SERVER ['request _ URI ']; // This is the address of this page.
$ Redirect_uri = urlencode ($ redirect_uri );
$ GetCodeUrl = 'https: // open.weixin.qq.com/connect/oauth2/authorize? Appid = '. $ appid.' & redirect_uri = '. $ redirect_uri.' & response_type = code & scope = snsapi_userinfo & state = oauth # wechat_redirect ';
Redirect ($ getCodeUrl, 0 );
} Elseif (! Empty ($ _ GET ['code']) & empty ($ _ SESSION ['oauth ']) {
$ GetAccesstokenUrl = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid.' & secret = '. $ appsecret.' & code = '. $ _ GET ['code'].' & grant_type = authorization_code ';
$ Res = WeChat_request ($ getAccesstokenUrl );
$ Openid = $ res-> openid;
$ _ SESSION ['oauth '] = $ openid;
Dump ($ openid );
// Obtain user information storage
} Else {
Dump ('second access ');
Dump ($ _ SESSION ['oauth ']);
}
Dump ('prove success when I see it ');
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.