thinkphp third-party login-QQ Login

Source: Internet
Author: User
Tags csrf attack

Reference: Think Dream education

1, QQ Internet (link address: https://connect.qq.com/index.html), for the developer Certification (not certified), there are two types of corporate and personal authentication methods, individual developers choose personal certification is good, ready for the electronic version of the identity card. Authentication interface One, certification does not do not repeat.

(Figure I)

2, certification through the interface two, certification, we will create the application of three, choose to create a website application, fill in the personal information four.

(Figure II)

(Figure III)

(Figure IV)

3, the application is created, there will be about 2 days of certification time, certification through the case, can be viewed under the application management. Successful creation of the application five

(Figure V)

4, Code section, tp3.2 version, new QqController.class.php, name self-proposed, but need to meet your callback address.

<?php/* * P:QQ Login * U:shenlin * t:2017-10-18 * q:297760026 * */namespace home\controller;use Think\Controller;class QqCo Ntroller extends controller{//Initiate request Public Function Qqsend () {//Parameter $url = "Https://graph.qq.com/oauth2        .0/authorize ";        $param [' response_type '] = "code";        $param [' client_id ']= "your AppID";        $param [' redirect_uri '] = "Your callback address";        $param [' scope '] = "get_user_info";        -------generates a unique random string anti-csrf attack $param [' state '] = MD5 (Uniqid (rand (), TRUE));        $_session[' state ') = $param [' state '];        Stitching URL $param = http_build_query ($param, "", "&"); $url = $url. "?".        $param;    Header ("Location:". $url);        }//Callback Public Function Qqback () {$code = I (' Get.code ');        $state = I (' get.state '); if ($code && $state = = $_session[' state ') {//get Access_token $res = $this->getaccesstoken (            $code, "Your AppID", "Your Appkey");            Dump ($res);            Exit (); Parse_str ($res, $data);            $access _token = $data [' Access_token '];            $url = "Https://graph.qq.com/oauth2.0/me?access_token = $access _token";            $open _res = $this->httpsrequest ($url);                if (Strpos ($open _res, "callback")!== false) {$lpos = Strpos ($open _res, "(");                $rpos = Strrpos ($open _res, ")");            $open _res = substr ($open _res, $lpos + 1, $rpos-$lpos-1);            } $user = Json_decode ($open _res);            $open _id = $user->openid; $url = "https://graph.qq.com/user/get_user_info?access_token= $access _token&oauth_consumer_key= your appid&            open_id = $open _id ";            $user _info = $this->httpsrequest ($url);        Output QQ User information dump ($user _info);    } $this->display (); }//through authorization code get Access Token Public function Getaccesstoken ($code, $app _id, $app _key) {$url = "https://        Graph.qq.com/oauth2.0/token "; $param [' Grant_type ']= ' AuthorizAtion_code ";        $param [' client_id ']= $app _id;        $param [' Client_secret ']= $app _key;        $param [' Code ']= $code;        $param [' Redirect_uri ']= "your callback address";        $param =http_build_query ($param, "", "&"); $url = $url. "?".        $param;    return $this->httpsrequest ($url);        }//httpsrequest Public Function httpsrequest ($post _url) {$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $post _url);//address to be accessed curl_setopt ($ch, Curlopt_returntransfer, 1);//Whether the execution result is returned, 0 is returned, and 1 is not returned        Back to Curl_setopt ($ch, Curlopt_timeout, 30);//Set Timeout $res = curl_exec ($ch);//execute and get data return $res;    Curl_close ($ch); }}

  

thinkphp third-party login-QQ Login

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.