Access PHP session variable from different paths

I am facing a very strange problem in PHP sessions, here are the facts:

Creating session variable code - mydomain / a / b / c / create_session.php

<? session_start(); $_SESSION['test'] = "Hello World"; ?> 

Reading the session variable read mydomain / a / b / c / read_session.php

 <? session_start(); echo $_SESSION['test']; ?> 

Problem

When I access read_session.php code from the same URL, it works fine. But when I try to read the session variable from another path, it does not work.

<strong> Examples

mydomain / a / b / c / read_session.php - works!

mydomain / a / b / read_session.php - works!

mydomain / a / read_session.php - works!

mydomain / read_session.php - does not work!

mydomain / d / read_session.php - does not work!

+4
source share
4 answers

This may not be a problem.

Domains must be exaccty the same (cookie policy), i.e. http: //www.domain does not match http: // domain

+1
source

You can try using session_set_cookie_params if you have a configuration setting, things get confused somewhere:

 session_set_cookie_params(0, '/'); session_start(); 

You can set cookies to be used only on certain paths in the domain. Perhaps this happened here. Please note: if this is a problem, the best way to fix it is to change the value in php.ini :

 session.cookie_path = "/" 
0
source

As Bridis says, you cannot use the same domains. They were the same.

Be sure to check session.cookie_paths . Perhaps your configuration settings set the path to the cookie to "/ a". In this case, the functionality that you described will take place, as there will be a path mismatch.

0
source

Cookies (and therefore cookies for session identifiers) can be tied to specific domains and paths. The default cookie session cookie configuration for PHP is to bind cookies to the current domain ( session.cookie_domain ) and the path / ( session.cookie_path ).

Perhaps your session configuration is different from the default configuration because the cookie path is set to /a , so that the cookie is only valid in /a , as well as those paths where /a is the correct path prefix (i.e. /a/… ) .

Try changing the path to the cookie / :

 ini_set('session.cookie_path', '/'); 
0
source

Source: https://habr.com/ru/post/1339200/


All Articles