<?php
session_start();

header("Content-type: text/html; charset=utf-8");
$home = 'index.php';
class db{
    private $host;
    private $user;
    private $pass;
    private $database;
    private $charset;
    function __construct($host,$user,$pass,$database,$charset){
        $this->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->database=$database;
        $this->charset=$charset;
        $this->connect();
    }
    function connect(){
        mysql_connect($this->host,$this->user,$this->pass) or die ("连接数据库服务器失败!");
        mysql_select_db($this->database) or die ("连接数据库失败!");
        mysql_query("set names $this->charset");        
    }
    function select($sql){
        $select=mysql_query($sql);
        $rows = '';
        while($row = mysql_fetch_array($select, MYSQL_ASSOC)) {
        $rows[] = $row;
        }
        return $rows;
    }
    function insert($tab,$col,$value){
        mysql_query("INSERT INTO $tab($col)values($value)");
        return mysql_insert_id();
    }
    function update($tab,$col,$new_value,$colm,$value){
        mysql_query("UPDATE $tab SET $col=$new_value where $colm=$value");    
    }
    function delete($tab,$col,$value){
      mysql_query("DELETE FROM $tab where $col=$value");
    }
    function close(){
    mysql_close();
    }
}
$db = new db('127.0.0.1','root','lizhifeng','jinba','utf8');

//通过oauth2授权获取用户详细信息
//$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx954b290311defa52&redirect_uri=http://jinba2.emailcar.net&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';

if(!$_SESSION['userid']){
if (isset($_GET['code'])){
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx954b290311defa52&secret=299a1a9ba0db6d1b09266842de62079c&code='.$code.'&grant_type=authorization_code';
$json = file_get_contents($url); 
$arr = json_decode($json,true);
$token = $arr['access_token'];  
$openid = $arr['openid'];
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$token.'&openid='.$openid.'&lang=zh_CN'; 
$json = file_get_contents($url); 
$arr = json_decode($json,true); 
$name = $arr['nickname'];
$imgURL = $arr['headimgurl'];
$sex = $arr['sex'];
$province = $arr['province'];
$city= $arr['city'];
$country= $arr['country'];
$result = $db->select('select * from member where openid = "'.$openid.'"');
if($result){
$userid = $result[0]['id'];
$_SESSION['userid'] = $userid;
}else{
if($openid){
$userid = $db->insert('member','openid,nickname,headimgurl,sex,city,country,province,create_time','"'.$openid.'","'.$name.'","'.$imgURL.'","'.$sex.'","'.$city.'","'.$country.'","'.$province.'","'.time().'"');
$_SESSION['userid'] = $userid;
}else{
header("Location: $url"); 
}
}
}else{
    header("Location: $url"); 
}
}
$userid = $userid?$userid:$_SESSION['userid'];

微信oauth2授权获得用户信息的更多相关文章

  1. php微信网页授权获取用户信息

    配置回调域名: 1. 引导用户进入授权页面同意授权,获取code 2. 通过code换取网页授权access_token(与基础支持中的access_token不同) 3. 如果需要,开发者可以刷新网 ...

  2. 微信OAuth授权获取用户OpenId-JAVA(个人经验)【申明:来源于网络】

    微信OAuth授权获取用户OpenId-JAVA(个人经验)[申明:来源于网络] 地址:https://my.oschina.net/xshuai/blog/293458

  3. 微信小程序 获取用户信息并保存登录状态

    微信小程序 获取用户信息并保存登录状态:http://www.360doc.com/content/18/0124/11/9200790_724662071.shtml

  4. Laravel wxxcx 微信小程序获取用户信息

    wxxcx 是Laravel5微信小程序登录获取用户信息扩展 部署 12345678 # 安装$ composer require iwanli/wxxcx# 注册服务# 在 /config/app. ...

  5. 微信 OAuth2 网页授权获取用户信息

    文档:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html !!! 微信跟用户没有关系类接口采用了OAUTH2 [ ...

  6. 微信 公众号 小程序 授权 unionid 用户信息 实验总结

    -*-*-*-*-*-*-*-*-*--*-*-*-1.小程序通过code获取用户openid的接口,如果用户曾经授权并未过期,或者用户关注过同主体的公众号,会带回unionID,但没有用户头像等信息 ...

  7. 微信公众号授权回调用户信息,获取openid

    1.--------------------用户授权登录并获取code 授权登录方式有两个,一种为静默授权登录(scope=snsapi_base),一种为非静默授权登录(scope=snsapi_u ...

  8. 微信网页授权获取用户openid及用户信息

    $code = $_GET["code"];//获取code $appid=“xxxx”;//公众号appid $APPSECRET="xxx";//公众号ap ...

  9. 微信小程序 获取用户信息授权

    login.wxml界面 获取用户授权可以用微信提供的接口 wx.authorize, 但是获取用户信息的授权用这个接口不会弹出授权窗口 <button class="submit-b ...

随机推荐

  1. Enum应用

    public enum ZDJGJD { YSZ("01",0.3,"取得预售许可"),JGFD("02",0.6,"单位结构封顶 ...

  2. css实现三角箭头(兼容IE6)

    纯css实现三角箭头有几种方式,常规的方式用css3的rotate把元素旋转45度角,无法兼容ie的主要原因是ie不支持边框透明, 第二种方法,使用chroma滤镜透明,经尝试在ie下会出现activ ...

  3. Codeforces 459C Pashmak and Buses 机智数学题

    这个题目说的是有n个人,有k辆巴士,有m天,每天都要安排n个人坐巴士(可以有巴士为空),为了使得这n个人不会成为朋友,只要每两个人在这m天里坐的巴士至少一天不相同即可. 要你求是否有这样的安排方法,如 ...

  4. 超级简单 一分钟实现react-native屏幕适配

    今天因为react-native的style只能给width和height设置数字 没有react上的vw和vh 因为之前经常用vh vw 感觉不适应 找到了一个新的方法 使用Demension模块 ...

  5. GS 原理及破解 《0day安全》

    1.原理: 在main函数之前,会调用__security_init_cookie函数(win10,vs2017,release,x86): 进入__security_init_cookie函数内部: ...

  6. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  7. 洛谷 P2516 [HAOI2010]最长公共子序列

    题目传送门 解题思路: 第一问要求最长公共子序列,直接套模板就好了. 第二问要求数量,ans[i][j]表示第一个字符串前i个字符,第二个字符串前j个字符的最长公共子序列的数量 如果f[i][j]是由 ...

  8. JS - 使 input 失去焦点

    $(document).ready(function(){ $("body").click(function(){ if(!event.srcElement.type) {     ...

  9. tc: 模拟网络异常的工具

    作者:smallnest Linux Traffic Control (tc)的扩展 Network Emulation (netem)可以很方便的模拟网络不好的情况,一般新的linux内核中(> ...

  10. Python之日志处理(logging模块)转载

    本人主要做一个知识的归类与记录,如是转载类文章,居首都会备注原链接,尊重原创者,谢谢! 此文转载原链接:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 ...