PDOSession的奇怪行为

时间:2012-11-20 14:47:28

标签: php session pdo

我的PDOSession有问题并且正在寻找一些帮助调试(BTW这似乎只发生在IE上)

我有两个文件供测试 index.php和session2.php(在下面)

最后我在index.php中的

---> header(“Location:session2.php”);

偶尔浏览器进入状态 当我去session2.php时,sessionid是不同的,当然会话数据消失了

我有一个非常简单的PDOSession类,见下文

这是日志文件中的调试语句。出于某种原因,当我去session2.php它有一个不同的会话ID。 (OPEN / READ和CLOSE来自PDOSESSION类)

我不确定这是否足以让任何人帮忙,但我真的很难过......

感谢您的帮助

[PDOSession] index.php OPEN 
[PDOSession] index.php READ id=vpodh6fornta10mg2lqkns2qb1
[11.20.12 8:33:09][session1]
[11.20.12 8:33:09][session1]->just before forwarding to session2.php
[11.20.12 8:33:09][session1][vpodh6fornta10mg2lqkns2qb1] Array
(
[TstpageID] => 1
)

[PDOSession] index.php WRITE id=vpodh6fornta10mg2lqkns2qb1
[PDOSession] index.php CLOSE 
[PDOSession] session2.php OPEN 
[PDOSession] session2.php READ id=f7s4u3bc1hbo0s0dj3s9o8g1u7
[11.20.12 8:33:09][session2]
[11.20.12 8:33:09][session2][f7s4u3bc1hbo0s0dj3s9o8g1u7] Array
(
)

[PDOSession] session2.php WRITE id=f7s4u3bc1hbo0s0dj3s9o8g1u7
[PDOSession] session2.php CLOSE 

这是index.php          

require 'functions.php';
$link = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWD);
$link->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

PDOSession::init($link);    
errorLog("[session1]\n");

$pageID = 1 ;

$_SESSION['TstpageID'] = $pageID;
errorLog("[session1]->just before forwarding to session2.php\n");
errorLog("[session1][".session_id()."] ".print_r($_SESSION,true)."\n");
header("Location: session2.php");
?>

这是session2.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
set_include_path('/home/unitnetfb/html/inc');

$noFACEBOOK_INIT = 1;
require 'functions.php';
$link = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWD);
$link->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

PDOSession::init($link);    

errorLog("[session2]\n");

errorLog("[session2][".session_id()."] ".print_r($_SESSION,true)."\n");
?>

这是PDO SESSION类

<?php
abstract class PDOSession
{
    private static $db;
    private static $oldData;
    public static function init(PDO $db){
            self::$db = $db;
            session_set_save_handler('PDOSession::open', 'PDOSession::close',
                                     'PDOSession::read', 'PDOSession::write',
                                     'PDOSession::destroy', 'PDOSession::garbageCollect');
            session_start();
    }
    public static function open($save_path, $session_name){
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." OPEN \n" , 3, "/tmp/phperror.log");
            return true;
    }
    public static function close(){
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." CLOSE \n" , 3, "/tmp/phperror.log");
            return true;
    }
    public static function read($id){
        try{
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." READ id=".$id."\n" , 3, "/tmp/phperror.log");
            $query = self::$db->prepare('
                    SELECT data
                    FROM sessions
                    WHERE id = :id');
            $query->execute(array(':id' => $id));
            return $query->fetchColumn();

        }catch(Exception $e){
            die("A hardware failure has cause a loss of connection to our databases.  We apologize for the inconvenience. [read]");
        }
    }
    public static function write($id, $data){
        try{
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." WRITE id=".$id."\n" , 3, "/tmp/phperror.log");
             $t = time();
            $stmt = self::$db->prepare("REPLACE INTO
            `sessions`
            VALUES (:id, :accessTime, :data)");
            return $stmt->execute(array(':id' => $id, ':accessTime' => $t, ':data' => $data));
        }catch(Exception $e){
            die("A hardware failure has cause a loss of connection to our databases.  We apologize for the inconvenience. [write]");
        }
    }
    public static function destroy($id){
        try{
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." DESTROY id=".$id."\n" , 3, "/tmp/phperror.log");
            self::$db
                    ->prepare('
                            DELETE FROM sessions
                            WHERE id = :id')
                    ->execute(array(':id' => $id));

        }catch(Exception $e){
            die("A hardware failure has cause a loss of connection to our databases.  We apologize for the inconvenience. [destroy]");
        }
    }
    public static function garbageCollect($lifetime){
        try{
            error_log("[PDOSession] ".$_SERVER["SCRIPT_NAME"]." GARBAGECollect\n" , 3, "/tmp/phperror.log");
            $t = time();
            self::$db
                    ->prepare('
                            DELETE FROM sessions
                            WHERE accessTime < :min_time')
                    ->execute(array(':min_time' => $t - $lifetime));
            errorLog("[".date("m.d.y G:i:s")."][garbageCollect]"." lifetime=$lifetime accessTime < ".$t - $lifetime );
        }catch(Exception $e){
            errorLog("[".date("m.d.y G:i:s")."][garbageCollect]"." Exception -->".$e->getMessage(),$e);
            die("A hardware failure has cause a loss of connection to our databases.  We apologize for the inconvenience. [gc]");
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

问题是与IE的iframe中的第三方Cookie

标题('P3P:CP =“IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT”');

解决了问题