php QQ登录 session['state'] 一直找不到 求解
易联主机的虚拟空间(不知道跟这个有没有关系)
使用QQsdk php 做QQ登录
按照步骤设置config和session文件
结束只有一个
//session.php 代码
<?php
/**
* PHP SDK for QQ登录 OpenAPI
*
* @version 1.2
* @author connect@qq.com
* @copyright © 2011, Tencent Corporation. All rights reserved.
*/
/**
* @brief 设置session配置
*/
/**
* CREATE TABLE `tbl_session` (
* `session_id` varchar(255) binary NOT NULL default '',
* `session_expires` int(10) unsigned NOT NULL default '0',
* `session_data` text,
* PRIMARY KEY (`session_id`)
* ) ENGINE=MyISAM;
*/
class Session
{
//mysql的主机地址
const db_host = "localhost"; //需要第三方指定ip地址
//数据库用户名
const db_user = "ibrat"; //需要第三方指定自己的用户名
//数据库密码
const db_pwd = "ibrat"; //需要第三方指定自己的库据库密码
//数据库
const db_name = "ibrat"; //需要第三方指定数据库
//数据库表
const db_table = "ghb_session"; //需要第三方指定数据表
//mysql-handle
private $db_handle;
//session-lifetime
private $lifeTime;
function open($savePath, $sessName)
{
// get session-lifetime
$this->lifeTime = get_cfg_var("session.gc_maxlifetime");
// open database-connection
$db_handle = @mysql_connect(self::db_host, self::db_user, self::db_pwd);
$dbSel = @mysql_select_db(self::db_name, $db_handle);
// return success
if(!$db_handle || !$dbSel)
return false;
$this->db_handle = $db_handle;
return true;
}
function close()
{
$this->gc(ini_get('session.gc_maxlifetime'));
// close database-connection
return @mysql_close($this->db_handle);
}
function read($sessID)
{
// fetch session-data
$res = @mysql_query("SELECT session_data AS d FROM ".self::db_table."
WHERE session_id = '$sessID'
AND session_expires > ".time(), $this->db_handle);
// return data or an empty string at failure
if($row = @mysql_fetch_assoc($res))
return $row['d'];
return "";
}
function write($sessID, $sessData)
{
// new session-expire-time
$newExp = time() + $this->lifeTime;
// is a session with this id in the database?
$res = @mysql_query("SELECT * FROM ".self::db_table."
WHERE session_id = '$sessID'", $this->db_handle);
// if yes,
if(@mysql_num_rows($res))
{
// ...update session-data
@mysql_query("UPDATE ".self::db_table."
SET session_expires = '$newExp',
session_data = '$sessData'
WHERE session_id = '$sessID'", $this->db_handle);
// if something happened, return true
if(@mysql_affected_rows($this->db_handle))
return true;
}
else // if no session-data was found,
{
// create a new row
@mysql_query("INSERT INTO ".self::db_table." (
session_id,
session_expires,
session_data)
VALUES(
'$sessID',
'$newExp',
'$sessData')", $this->db_handle);
// if row was created, return true
if(@mysql_affected_rows($this->db_handle))
return true;
}
// an unknown error occured
return false;
}
function destroy($sessID)
{
// delete session-data
@mysql_query("DELETE FROM ".self::db_table." WHERE session_id = '$sessID'", $this->db_handle);
// if session was deleted, return true,
if(@mysql_affected_rows($this->db_handle))
return true;
// ...else return false
return false;
}
function gc($sessMaxLifeTime)
{
// delete old sessions
@mysql_query("DELETE FROM ".self::db_table." WHERE session_expires < ".time(), $this->db_handle);
// return affected rows
return @mysql_affected_rows($this->db_handle);
}
}
/**
* 指定session有效的域名
* ini_set("session.cookie_domain", ".domain.com");
* .domain.com是站点的主域名,请注意前面个有一个'.'
*/
define("MAIN_DOMAIN", ".gahaobeia.com"); //设置主域名
/**
* 不同子域名下共享session信息
* COOKIE_DOMAIN = false 禁止该功能
* COOKIE_DOMAIN = true 启用该功能
* 默认禁止
* 开启前提需要定义MAIN_DOMAIN常量
*/
define("COOKIE_DOMAIN", false);
if (defined("COOKIE_DOMAIN") && COOKIE_DOMAIN)
{
if (defined("MAIN_DOMAIN"))
@ini_set("session.cookie_domain", MAIN_DOMAIN);
}
/**
* 同一个主域名,不同服务器之间共享session信息
* USER_SESSION = false 禁用该功能
* USER_SESSION = true 启用该功能
* 默认禁止
* 开启前提需要建立mysql数据表
*/
define("USER_SESSION", true);
if (defined("USER_SESSION") && USER_SESSION)
{
@ini_set("session.save_handler", "user");
$session = new Session;
@session_module_name("user");
@session_set_save_handler(
array(&$session, "open"),
array(&$session, "close"),
array(&$session, "read"),
array(&$session, "write"),
array(&$session, "destroy"),
array(&$session, "gc"));
}
//@session_id("demo");
session_save_path("/tmp");
session_start();
?>
//config.php 代码
<?php
/**
* PHP SDK for QQ登录 OpenAPI
*
* @version 1.2
* @author connect@qq.com
* @copyright © 2011, Tencent Corporation. All rights reserved.
*/
/**
* @brief 本文件作为demo的配置文件。
*/
/**
* 正式运营环境请关闭错误信息
* ini_set("error_reporting", E_ALL);
* ini_set("display_errors", TRUE);
* QQDEBUG = true 开启错误提示
* QQDEBUG = false 禁止错误提示
* 默认禁止错误信息
*/
define("QQDEBUG", true);
if (defined("QQDEBUG") && QQDEBUG)
{
@ini_set("error_reporting", E_ALL);
@ini_set("display_errors", TRUE);
}
/**
* session
*/
include_once("session.php");
/**
* 在你运行本demo之前请到 http://connect.opensns.qq.com/申请appid, appkey, 并注册callback地址
*/
//申请到的appid
//$_SESSION["appid"] = yourappid;
$_SESSION["appid"] = 100247949;
//申请到的appkey
//$_SESSION["appkey"] = "yourappkey";
$_SESSION["appkey"] = "740f169aab353c757f02134e682b5511";
//QQ登录成功后跳转的地址,请确保地址真实可用,否则会导致登录失败。
//$_SESSION["callback"] = "http://your domain/oauth/get_access_token.php";
$_SESSION["callback"] = "http://gahaobeia.com/qqlogin/oauth/qq_callback.php";
//QQ授权api接口.按需调用
$_SESSION["scope"] = "get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo";
//print_r ($_SESSION);
?>
[解决办法]
初始化变量的工作要放在代码的最上面,因为在请求QQ API的方法里初始化,会造成SESSION为空