簡體   English   中英

無法訪問會話數據

[英]Cannot access session data

在我的項目中,我試圖從位於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參數:

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

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

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

為什么會這樣?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM