简体   繁体   中英

Use the same PHP session on two different folders on the same server

I have two folders on my webspace Folder1 and Folder2 . Both folders have PHP content in it. In Folder1 's index.php I'm setting a session variable which I want to be known in Folder2 . I already searched for a solution and I also found something with session_set_cookie_params() - but it's not working. Here are the files I'm working with:

<?php

// index.php inside /blog/Folder1/

session_name('s');
session_set_cookie_params(0, '/', 'www.mydomain.de');
session_cache_limiter('private_no_expire');
session_start();
$_SESSION['auth_key'] = 'abc';
echo 'Key assigned';

?>

And my second file which is trying to access the already set session variable:

<?php

// index.php inside /blog/Folder2/

session_name('s');
session_set_cookie_params(0, '/', 'www.mydomain.de');
session_cache_limiter('private_no_expire');
session_start();

if($_SESSION['auth_key'] != 'abc')
{
    header('HTTP/1.0 404 Not Found');
    exit;
}

// do more stuff ...

?>

What am I doing wrong?

There's nothing wrong with your php, but to debug this you need to understand this, for security reason http will not set cookie for top level domains, as a result you need to use second level domain as follow:

session_set_cookie_params(0, '/', '.mydomain.de');

you noticed I replaced www.mydomain.de by .mydomain.de

A little more

make sure you have the domains set up if they are not you can still use session_set_cookie_params(0, '/'); without indicating the subdomain index.

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