Does a PHP session work in subdirectories?

I have a main directory called System with a subdirectory named Subsystem . My session from the main directory does not work in a subdirectory.

When I echo session_save_path(); in both folders, they show me "/tmp" .

Then I tried to put session_save_path("../tmp"); to your subdirectory, but it shows me "This webpage has a redirect loop" .

session.php in the System directory:

 <?php session_start( ); if (!($_SESSION['uid'])) { header("Location:index.php"); } else { $_SESSION['uid'] = $_SESSION['uid']; } ?> 

session.php in the subsystem folder:

 <?php session_save_path("../tmp"); session_start( ); if (!($_SESSION['uid'])) { header("Location:index.php"); } else { $_SESSION['uid'] = $_SESSION['uid']; } 

? >

I have Googled all over, but I still can't get it to work.

+6
source share
2 answers

The directory does not affect your session state (all directories on this Apache-PHP site will access the same session in the standard configuration). You do not need to use session_save_path() .

I think the problem is partially that you are setting the β€œuid” for yourself ( $_SESSION['uid'] = $_SESSION['uid']; ) - so you never potentially set it to a value - and maybe redirect endlessly if it is not installed.

I suggest this simple test to make sure your sessions really work:

/session_set.php

 <?php session_start(); $_SESSION['uid'] = 123; 

/sub_dir/session_get.php

 <?php session_start(); echo $_SESSION['uid']; 
+1
source

The session creates a file in a temporary directory on the server where the registered session variables and their values ​​are stored. This data will be available for all pages of the site during this visit.

The location of the temporary file is determined by the setting in the php.ini file named session.save_path. therefore, please check this path.

Also [session-save-path ()] [1] Get and / or set the current session save path.

I think you do not need to write this line and check your php.ini for the correct path.

for the session, I found a useful article http://www.tutorialspoint.com/php/php_sessions.htm

Thanks.

0
source

All Articles