<?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. 二十五、JavaScript之查找字符串中的字符串indexOf和lastIndexOf的用法

    一.代码如下 二.效果如下 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" conten ...

  2. WinForm读写App.config配置文件

    一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  3. 记录一次ajax使用

    function getSSOUrl(urlvalue) { var urls; $.ajax({ type: "GET", url: "../../GeoManage/ ...

  4. qt 中使用 c 语言文件

    qt 中直接使用 c 语言文件,c 文件可以直接包含,h 文件包含的时候,需要在 c++ 中添加额外信息,如下: #ifdef __cplusplus extern "C" { # ...

  5. 设置进程用指定IE版本

    function IsWOW64: BOOL; begin Result := False; if GetProcAddress(GetModuleHandle(kernel32), 'IsWow64 ...

  6. 使用docker快速体验kali linux

    环境 运行在 64位 机器 企业版的 win10 系统 下载镜像 首先搜索docker download 去官网下载docker:https://www.docker.com/products/doc ...

  7. SpringMVC:提交日期类型报400错误解决方法

    方法1:可以使用@ControllerAdvice增强Controller @ControllerAdvice public class BaseControllerAdvice { // 初始化绑定 ...

  8. Vue 改变数组触发视图更新

    最近给table做了一个点击排序的功能,数组更改以后发现data数据变了,但是视图不更新 写惯了js的我们随手一串代码 this.items[2]={message:"Change Test ...

  9. webservice 的简单实现

    1.什么是webservice: 服务端整出一些资源让客户端访问(获取数据) 一个跨语言.跨平台的规范2.作用:跨平台调用.跨语言调用.远程调用 3.什么时候使用webservice: 1.新旧系统之 ...

  10. Day2-T2

    原题目 Describe:贪心,保证至少一条路牛的数量最多 code: #include<bits/stdc++.h> using namespace std; long long n,m ...