2011-12-23 87 views
1

在我的項目,我想從2檔訪問會話數據,位於2名不同的目錄:無法訪問會話數據

/site/page.extension.php  <-- initializes the session and writes data to it 
           - also sets a cookie with session_id() and session_name() 
/extension/ajax_handler.php <-- tries to access the session data, session_id() 
           - and session_name() are set via cookie and return the correct values 

現在,我的問題是,即使SESSION_ID()和session_name()在兩個文件中都是相同的,我不能訪問會話數組,它只是返回一個空數組。

我的代碼: page.extension.php:

session_start(); 
setcookie("psc_session", session_id(), strtotime("+20 minutes"), "/"); 
setcookie("psc_session_name", base64_encode(session_name()), strtotime("+20 minutes"), "/"); 
$_SESSION['uid'] = system::current_user_id(); 

ajax_handler.php:

session_id($_COOKIE['psc_session']); 
session_name(base64_decode($_COOKIE['psc_session_name'])); 
session_start(); 
print_r($_SESSION); // => array(0) { } 

我真的很感激任何幫助! 問候!

更新: 我試過設置在page.extension.php使用這個會話cookie PARAMS:

$url = str_replace("http://", '', current_url(false)); // returns the current domain 
session_set_cookie_params(10800, "/", $url, 0, 1); 

如果我現在訪問我收到(在ajax_handler.php)session_get_cookie_params:

print_r(session_get_cookie_params()); // => 
Array 
(
    [lifetime] => 0 
    [path] =>/
    [domain] => 
    [secure] => 
    [httponly] => 
) 

這是爲什麼發生?

+0

什麼是'system :: current_user_id();'? – Neal 2011-12-23 13:56:06

+0

它只是返回當前登錄用戶的數字ID(cookie) – n0pt3x 2011-12-23 13:57:24

+0

這裏有個有趣的方法是什麼原因? – Neal 2011-12-23 14:01:36

回答

0

我無法複製您的問題,重新創建您提供會話變量的代碼,並且cookie保持不變,可從ajax_handler.php訪問。我建議你回溯並確保兩個文件都是從同一個域請求的。