<?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. XPath--快速获取XML数据的节点或属性

    转载自 XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识.示例Xml: <?xml version="1.0" ...

  2. 汇编,寄存器,内存,mov指令

    一.代码 和 汇编 和 二进制之间的关系 二.复习一下计算机组成原理的知识 1.寄存器 计算机中有三个存储 32位cpu提供的寄存器有三种类型8位 16位 32位 64位的只是32位的扩展 并且程序大 ...

  3. SFINAE 与 type_traits

    SFINAE 与 type_traits SFINAE 替换失败不是错误 (Substitution Failure Is Not An Error),此特性被用于模板元编程. 在函数模板的重载决议中 ...

  4. 网络基础:OSI 七层模型、TCP/IP 四层模型

    1.Internet历史 1. 1968年由美国ARPA机构提出"资源共享计算机网络”,让ARPA的计算机互联起来,叫做阿帕网;2. 1974年,第一个TCP协议详细说明发布了.3. 一个 ...

  5. 将本地项目映射到外网(tomcat web项目)

    1.首先如上图下载nat123软件,该软件可以免费内网映射. 2.根据上面图片,按要求填写即可:内网端口:自己web服务对应端口,内网地址:本机地址可以使默认localhost,内网ip,其余默认即可 ...

  6. Transaction Managament(事务管理二、Spring事务)

    Transaction Managament(事务管理二.Spring事务) Spring事务框架的优势 ​ Spring事务框架将开放过程中事务管理相关的关注点进行了分离,对这些关注点进行了抽象分离 ...

  7. OC项目加入swift第三方库遇到的坑

    https://www.jianshu.com/p/96d868dcd69c 2017.07.07 16:23* 字数 295 阅读 5218评论 2喜欢 4 首先,在OC项目的Podfile文件中添 ...

  8. POJ - 3977 Subset(二分+折半枚举)

    题意:有一个N(N <= 35)个数的集合,每个数的绝对值小于等于1015,找一个非空子集,使该子集中所有元素的和的绝对值最小,若有多个,则输出个数最小的那个. 分析: 1.将集合中的元素分成两 ...

  9. CSS样式实现两个图片平分三角

    <div class='pageOption'> <a href='#' class='option' > <img src='http://imgsrc.hubbles ...

  10. 使用html画爱心

    <!DOCTYPE html"> <html> <head> <meta http-equiv="Content-Type" c ...