Record only: Micro-credit get nickname Auto Login
After several repeated verification, I found this method is flawed:
The micro-letter did not pay attention to access to the site, can not get nickname.
After paying attention, I can get a nickname with this method.
Is the nickname not generated because of the first generation of OpenID? Pending Test.
/**
* Get the full URL address of the current page
*/
function Get_url () {
$sys _protocal = isset ($_server[' Server_port ']) && $_server[' server_port '] = = ' 443 '? ' https://': ' http://';
$php _self = $_server[' php_self ']? $_server[' php_self ']: $_server[' script_name '];
$path _info = isset ($_server[' path_info '))? $_server[' Path_info ']: ';
$relate _url = isset ($_server[' Request_uri '))? $_server[' Request_uri ': $php _self. (Isset ($_server[' query_string '])? '?'. $_server[' query_string ']: $path _info);
Return $sys _protocal. (Isset ($_server[' http_host '])? $_server[' Http_host ']: $relate _url;
}
$wxch _config = $db-> getRow ("select * from ' ecs_weixin_config ' WHERE ' id ' = 1");
$appid = $wxch _config[' AppID '];
$appsecret = $wxch _config[' Appsecret '];
$APPID = $appid;
$SCRETID = $appsecret;
if (!$_session[' user_id '] && strpos ($_server[' http_user_agent '], ' Micromessenger ')!== false) {
if (!isset ($_get[' Code '))
{
$backurl = Get_url ();
$url = $jsApi->createoauthurlforcode ($backurl);
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=". $APPID. " &redirect_uri= ". UrlEncode ($backurl)." &response_type=code&scope=snsapi_base&state=123#wechat_redirect ";
echo $url;
Header ("Location: $url");
}else
{
Get the code code to get OpenID
$code = $_get[' code '];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $APPID. " &secret= ". $SCRETID." &code= ". $code." &grant_type=authorization_code ";
$re = Curl_get_contents1 ($url);
$rearr = Json_decode ($re, true);
$openid = $rearr [' OpenID '];
Var_dump ($rearr);
$jsApi->setcode ($code);
$openid = $jsApi->getopenid ();
$user _name = $db->getone ("Select uname from ecs_weixin_user where Wxid = ' {$openid} '");
if ($openid &&! $user _name) {//Register entry
$PASSW = MD5 (' shanmao.me '. Rand (1,18650144002));
$wxch _user_sql = "INSERT into ' ecs_weixin_user ' (' Wxid ', ' Setp ') VALUES (' $openid ', ' 3 ')";
$db-> Query ($wxch _user_sql);
$ecs _user_id = $db-> insert_id ();
if ($ecs _user_id<=0) {
Exit (' Error get insert_id ');
}
$url 3 = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=". $APPID. " &secret= ". $SCRETID;
$re 3 = curl_get_contents1 ($url 3);
$re 3arr = Json_decode ($re 3,true);
$token = $re 3arr[' Access_token '];
$url 2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $token. " &openid= ". $openid." &LANG=ZH_CN ";
$re 2 = curl_get_contents1 ($url 2);
$rearr 2 = Json_decode ($re 2,true);
$uc _username = $rearr 2[' nickname ']? $rearr 2[' nickname ': ' Doubag '. $ecs _user_id;
$time = Gmtime ();
$user _sql = "INSERT into ' ecs_users ' (' user_name ', ' password ', ' Reg_time ') VALUES (' $uc _username ', ' $passw ', ' $time ')";
$db-> Query ($user _sql);
$UC _update = "Update ecs_weixin_user SET ' uname ' = ' $uc _username ' WHERE ' uid ' = ' $ecs _user_id '";
$db-> Query ($uc _update);
$user->set_session ($uc _username);
$user->set_cookie ($uc _username,1);
Update_user_info ();
/*
$up _uid = Get_affiliate ();
if ($up _uid>0) {
$sql = ' UPDATE ecs_users SET parent_id = '. $up _uid. ' WHERE user_id = '. $ecs _user_id;
$db->query ($sql);
Header (' location:user.php?newuser=1 ');
}*/
}else{
$user->set_session ($user _name);
$user->set_cookie ($user _name,1);
Update_user_info ();
}
Setcookie ("Sopenid", $openid, Time () +864000, '/');
}
}
Var_dump ($openid);
function Curl_get_contents1 ($url) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_timeout, 2);
curl_setopt ($ch, Curlopt_useragent, "IE 6.0");
curl_setopt ($ch, Curlopt_referer, "");
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_ssl_verifypeer, FALSE);
curl_setopt ($ch, Curlopt_ssl_verifyhost, false);
$r = curl_exec ($ch);
Curl_close ($ch);
return $r;
}