【文件属性】:
文件名称:处理session跨域几种方案
文件大小:74KB
文件格式:DOC
更新时间:2017-12-12 04:40:55
session跨域
class Session
{
//mysql的主机地址
const db_host = "localhost"; //需要第三方指定ip地址
//数据库用户名
const db_user = "root"; //需要第三方指定自己的用户名
//数据库密码
const db_pwd = ""; //需要第三方指定自己的库据库密码
//数据库
const db_name = "thinkphp"; //需要第三方指定数据库
//数据库表
const db_table = "tbl_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;
}