2012-12-20 41 views
4

我在設置session_set_save_handler時遇到問題。我配置我的php.ini session.handler =用戶Session_set_save_handler未設置

這個簡單的測試失敗:

//Define custom session handler 
if(session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write",  "sess_destroy", "sess_gc")){ 
die('set fine'); 
}else{ 
die('Couldn\'t set session handler'); 

這是我的會話課。

//Constructor 
function __construct(){ 

//Define custom session handler 
if(session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc")){ 
    die('set fine'); 
}else{ 
    die('Couldn\'t set session handler'); 
} 

//Start session 
session_start(); 
} 


//Custom session functions 
function sess_open($sess_path, $sess_name) { 

return true; 
} 

function sess_close() { 

return true; 
} 

function sess_read($sess_id) { 

//Query for session record in 
$results = $db->QuerySingleRow("SELECT data FROM sessions WHERE session_id = '$sess_id'"); 

//Check that record is returned 
if ($results != false) 
{ 
    //Session found, pull out data field value 
    $sess_data = $results->data; 

    //Grab current time 
    $CurrentTime = time(); 

    //Update session record with current timestamp 
    $db->Query("UPDATE sessions SET last_updated = $CurrentTime WHERE session_id = '$sess_id'"); 

    //Return 
    return $sess_data; 
} 
else 
{ 
    //No session found 

    //Grab current timestamp 
    $CurrentTime = time(); 

    //Insert new session to DB 
    $db->Query("INSERT INTO sessions (session_id, last_updated) VALUES ('$sess_id', $CurrentTime)"); 

    //Return blank per nature of session_set_save_handler read() 
    return ''; 
} 
} 

function sess_write($sess_id, $data) { 

//Grab current timestamp 
$CurrentTime = time(); 

//Update session record to hold new data and update last_updated field 
$db->Query("UPDATE sessions SET data = '$data', last_updated = $CurrentTime WHERE session_id = '$sess_id'"); 

return true; 
} 

function sess_destroy($sess_id) { 

//Delete session from DB 
$db->Query("DELETE FROM sessions WHERE session_id = '$sess_id'"); 

return true; 
} 

function sess_gc($sess_maxlifetime) { 

//Get current timestamp 
$CurrentTime = time(); 

//Delete from session based on garbage collection 
$db->Query("DELETE FROM sessions WHERE last_updated < $CurrentTime"); 

return true; 
} 


} 

我能想到的唯一的事情是$ DB是一個假設是我的MySQL數據庫類的對象,但我不能包括類,然後創建它的一個實例。

我不想重塑對DB類的車輪,所以我從傑夫威廉斯這裏抓住它:http://www.phpclasses.org/package/3698-PHP-MySQL-database-access-wrapper.html

我已經試過類體把那麼頁面不把它列入了側渲染,只是一個空白頁沒有任何錯誤。:

<?php 
include 'mysql.class.php'; 
$db = new MySQL(true); 

class session 
{ 

//Constructor 
function __construct(){ 
.... 

回答

2

設置會話保存處理失敗:

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc") 

因爲要登記做這些回調不存在:

var_dump(is_callable("sess_open")); # FALSE 

這是因爲您的對象方法需要正確註冊爲回調。一個對象方法回調函數是以包含兩個元素的數組的形式編寫的,第一個是對象,第二個是methodname的字符串。從PHP淨實施例,它類似於你的:

$handler = new FileSessionHandler(); 
session_set_save_handler(
    array($handler, 'open'), 
    array($handler, 'close'), 
    array($handler, 'read'), 
    array($handler, 'write'), 
    array($handler, 'destroy'), 
    array($handler, 'gc') 
); 

正如你可以看到,每個方法被寫爲與第一元件$handler總是單個陣列。

從課堂內部,您可以使用$this來引用同一個對象。但在您完全編寫自己的代碼之前,請檢查session_set_save_handler() PHP manual page以瞭解相關信息,示例和用戶貢獻的註釋。你可以通過不同的方式組織你的案例。

+0

Hakre,優秀的解釋非常感謝你! – AmarettoSlim

1

如果使用該函數的構造函數中,那麼你需要在$this通過像這樣:

session_set_save_handler(
    array($this, 'sess_open'), 
    array($this, 'sess_close'), 
    array($this, 'sess_read'), 
    array($this, 'sess_write'), 
    array($this, 'sess_destroy'), 
    array($this, 'sess_gc') 
); 

然後實例類

new SessionClass; 

每當您有疑問可以隨時取看看the documentation。一定要閱讀評論,他們通常非常有幫助。

+0

Sverri,謝謝你直接回到我的代碼集! – AmarettoSlim