Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a PHP session work across subdirectories?

Tags:

php

session

I have a main directory named System with a sub-directory named Subsystem. My session from main directory is not working in the sub-directory.

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

Then, I tried to put session_save_path("../tmp"); in my sub-directory but it shows me "This webpage has a redirect loop".

session.php in System directory:

<?php
session_start( );

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

session.php in Sub-system 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 cannot get it to work.

like image 457
Newbie Avatar asked Dec 05 '12 03:12

Newbie


1 Answers

The directory does not affect your session state (all directories of a given Apache-PHP website will access the same session in a standard configuration). You should not have to use session_save_path().

I think the problem in part is that you're setting 'uid' to itself ($_SESSION['uid'] = $_SESSION['uid'];) - therefore potentially never actually setting it to a value - and potentially redirecting indefinitely if it's not set.

I suggest this simple test to ensure that your sessions are, in fact, working:

/session_set.php

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

/sub_dir/session_get.php

<?php
    session_start();
    echo $_SESSION['uid'];
like image 90
Steven Moseley Avatar answered Sep 21 '22 18:09

Steven Moseley