简体   繁体   中英

Cannot access session data

In my project, I'm trying to access the session data from 2 files, located in 2 different directories:

/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

Now, my problem is, that even though session_id() and session_name() are the same in both files, I cannot access the session-array, it just returns an empty array.

My code: 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) { }

I would really appreciate any help! Greetings!

Update: I've tried setting the session cookie params using this in page.extension.php:

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

If I now access session_get_cookie_params I receive (in ajax_handler.php):

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

Why does this happen?

I cannot replicate your problem, recreating the code you supplied the session variables and the cookies remain intact and are accessible from the ajax_handler.php . I'd suggest you backtrack and make sure both files are requested from the same domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM