How to apply WeChat login to PHP website

Source: Internet
Author: User
Tags openid php website csrf attack


The recent development of the PC-side web site, the need to use the authorization login, considering the pre-mobile phone has been a large number of users, now thinking about the integration of resources to meet the same user data synchronization.

First, development information

1. Concept differentiation

Since exposure to the development of knowledge, will inevitably contact with the use of OpenID and Unionid, the following is an introduction to the official documents, please note that the distinction:

  • After the message interaction between the followers and the public, the public number can be given the OpenID of the followers (the encrypted number, each user is unique to the OpenID of each public number. For different public numbers, the same user's OpenID is different). The public number can be used to access basic user information based on OpenID, including nickname, Avatar, Gender, city, language, and time of concern.

  • Please note that if a developer has the need to unify user accounts across multiple public numbers or between public and mobile apps, the Unionid mechanism will be needed to meet these needs by going to the Open platform (open.weixin.qq.com) to bind the public number.

To summarize briefly:

  • OpenID is the identity of an ordinary user and is unique to the current developer account. An OpenID corresponds to a public number.

  • Unionid is a user-uniform identity. For applications under an open platform account, the Unionid of the same user is unique.

2. Inductive finishing

Around this circle, that is, the public number development and development platform development is different, want to allow the same user to use different platforms (such as PC, app, applet, etc.) website login, need to do account binding, and this binding account can not be differentiated by OpenID, and need to be differentiated by Unionid.

3. Typical issues

Appendix A common design problems, mainly in the pre-development of knowledge use is not comprehensive, and lead to the impact of subsequent expansion, of course, this is my problem, I hope to give you a wake-up call.

Second, open platform operation

1. Brief guide

According to the following requirements, the creation of "website Application Development" is selected, and then the application of the materials according to the official instructions will normally take more than three days.

When the app is created, it must also meet the access of the interface permissions, there will be staff active contact, usually one day can be completed.

2. Officially provided scene reference

3. Binding public accounts/small programs

In order to guarantee the use of Unionid bindings for the corresponding user under the same Development Account, the corresponding public number/service number must be bound in the following list, which is generally to meet the payment function.

4. Authorization to get Access_token timing diagram

Third, the code implementation

In fact, the main time spent in the pre-application operation, and the real code implementation is very simple, the following is my way of implementation, please criticize.

1. Public file Configuration

The customary primary configuration information is agreed to be placed in the configuration file, ' \application\common\conf\config.php '.

' Weixin_login ' = + Array (        //Open Platform Use Account login app or website configuration information        ' open_appid ' = ' wxbd961b2a6b7b2963 ',//Application APPID        ' Open_appsecret ' = ' e6xxxxxxxxxxxxxxxxxxxxe90 ',//Application Appsecret        ' open_callbackurl '/' Www.52zhenmi.com/Home/Login/wxBack ',//The user uses scan QR code and confirms login, PC-side jump path    ),

2. Core code

Public Function Wxindex () {//--login-----Generate a unique random string anti-CSRF attack $state = MD5 (Uniqid (rand (), TRUE)); $_session["wx_state"] = $state;        Save to session $callback = UrlEncode ($this->callbackurl); ' Https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code        &scope=scope&state=state#wechat_redirect '; $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid=". $this->appid. " &redirect_uri= ". $callback." &response_type=code&scope=snsapi_login&state= ". $state."        #wechat_redirect ";    Header ("Location: $wxurl");            } public Function Wxback () {if ($_get[' state ']!=$_session["Wx_state"]) {echo ' Sorry, network request failed ... ';        Exit ("5001"); } $url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $this->appid. ' &secret= '. $this->appsecret. ' &code= '. $_get[' code '. ' &grant_type=authorization_code ';        $arr = Curl_get_contents ($url); Get Access_token and OpenID $url = ' https://api.weixin.qq.com/sns/userinfo?access_token= '. $arr [' Access_token ']. ' &openid= '. $arr [' OpenID ']. '        &LANG=ZH_CN ';        $user _info = curl_get_contents ($url);    $this->dealwithwxlogin ($user _info); */** * @param $user _info */Public Function Dealwithwxlogin ($user _info) according to the authorized user's information        TODO Data processing var_dump ($user _info);    Die }

3. Front-end display

According to the official document introduction, you can directly access the authorization scan interface, can also be customized design, estimation of my brain pumping, nested login sweep code of the function of the whole day did not realize, in this had to use the default jump.

The display results are as follows:

Scan after successful login to page jump

Iv. Summary

  • 1. Based on the above function, the information of the logged-in user can be obtained, and the OpenID and Unionid can store the database for post-business processing.

  • 2. In the following elaboration of limited capacity, it is recommended to refer to the official development of documents, Google predecessors of dry goods experience ...

  • 3. See a good article on the Internet, suggestions for reference: Public number of users and site users of the binding scheme

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.