Newbie
Newbie

Reputation: 189

Does a PHP session work across subdirectories?

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.

Upvotes: 7

Views: 5912

Answers (2)

mohan.gade
mohan.gade

Reputation: 1105

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.

The location of the temporary file is determined by a setting in the php.ini file called session.save_path. so pleasse check this path.

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

I think u dont need to write this line and check your php.ini for correct path.

for session i found some useful article http://www.tutorialspoint.com/php/php_sessions.htm

Thanks.

Upvotes: 0

Steven Moseley
Steven Moseley

Reputation: 16325

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'];

Upvotes: 1

Related Questions