<?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. 转载-Logistic回归总结

     Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2013年11月) 1.引言 看了Stanford的Andrew Ng老师的机器学习公开课中关于Logistic Regress ...

  2. 一百一十、SAP的OO-ALV之四,定义屏幕相关变量和逻辑流

    一.代码如下,定义相关变量 二.来带屏幕页面,双击STATUS_9000和USER_COMMAND_9000,自动生成相应代码 三.点击是 四.会自动生产关联的Includ文件 五.我们自己创建一个M ...

  3. 050-PHP除法运算

    <?php $n=10/3; //除法运算 echo $n; //输出变量n的值 ?>

  4. mysql union 和union all

    http://www.w3school.com.cn/sql/sql_union.asp

  5. 逆向--C函数和汇编

    C函数和汇编 C代码 (编译工具gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609平台ubuntu i386 32位) int bar(int c ...

  6. 使用Oracle VM VirtualBox安装CentOS 7.6操作系统

    使用Oracle VM VirtualBox安装CentOS 7.6操作系统                                                               ...

  7. POJ - 3349 Snowflake Snow Snowflakes (哈希)

    题意:给定n(0 < n ≤ 100000)个雪花,每个雪花有6个花瓣(花瓣具有一定的长度),问是否存在两个相同的雪花.若两个雪花以某个花瓣为起点顺时针或逆时针各花瓣长度依次相同,则认为两花瓣相 ...

  8. mac安装vue没有使用权限不足

    ➜  ~ vue init webpack frontend ⠋ downloading template /usr/local/lib/node_modules/vue-cli/node_modul ...

  9. Dubbo与SpringCloud

    dubbo和springcloud都是微服务框架,各自有各自的注册中心. dubbo监控中心:zookeeper,redis 提供高性能和透明化的RPC远程调用方案,SOA服务治理方案. 核心部分: ...

  10. export环境变量

    /etc/profile和/etc/profile.d/区别 [root@zzx conf]# vim /etc/profile.d/tomcat.sh   添加如下内容再运行脚本就可以添加环境变量 ...