<?php
try{
$conn = new PDO("mysql:host=$DB_SERVER;dbname=$DB_NAME",$DB_USER,$DB_PASS);
}
class SessionManager {
var $life_time;
function SessionManager() {
global $conn;
$this->life_time = get_cfg_var("session.gc_maxlifetime");
// Register this object as the session handler
session_set_save_handler(
array( &$this, "open" ),
array( &$this, "close" ),
array( &$this, "read" ),
array( &$this, "write" ),
array( &$this, "destroy"),
array( &$this, "gc" )
);
}
function read( $id ) {
global $conn;
$data = "";
$time = time();
$newid = $id;
$sql = "SELECT session_data FROM session_tmp WHERE session_id=? AND expired_date > ?";
$q = $conn->prepare($sql);
$result = $q->execute(array($newid, $time));
while($r = $q->fetch(PDO::FETCH_ASSOC)){
$data = $row['session_data'];
}
return $data;
}
function write( $id, $data ) {
$time = time() + $this->life_time;
global $conn;
$newid = $id;
$newdata = $data;
$sql = "SELECT session_id FROM session_tmp WHERE session_id = ?"; // error happen here!!!!
$q = $conn->prepare($sql);
$result = $q->execute(array($newid));
return TRUE;
}
}
我添加global $conn;
并function read()
修复了错误。我不知道为什么global $conn;
无法修复错误function write()
。如何修复错误?