Jump to content

Session not carried between sub-folder and root


stuartmarsh

Recommended Posts

Hi all,

I have two PHP files, one in root and the other in a folder called admin.

 

If I set $_SESSION in a file in the admin folder and then view $_SESSION in any other file within the admin folder, everything is OK.

 

However, if I then go to a file in root $_SESSION is blank, and it is blank if I then go back to a file in admin.

 

This problem only exists on my hosting and doesn't happen on a local dev machine.  I've compared the php.ini session settings and there both the same.

 

Any ideas?

 

Files

admin/test.php

<?php
session_start();

if( empty($_SESSION) ) {
echo "Session not set<br />";
$_SESSION['test'] = "test";
}

echo "<pre>";var_dump($_SESSION);echo "</pre>";

 

/test.php

<?php
session_start();

echo "<pre>";var_dump($_SESSION);echo "</pre>";

 

Link to comment
Share on other sites

A url with the www. on it and a url without the www. on it is not switching domains, but sessions won't match both unless you set the session cookie parameters correctly.

 

It sounds like your code on the page in the root folder is clearing the session variables. You would need to post your code if you want help with what it is doing.

Link to comment
Share on other sites

I've echoed session_id and it remains the same at all times.

 

Only one .htaccess in the root, code below:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.domain\.co\.uk" [R=301,L]

#Route HTML pages through index.php (page.html -> index.php?page)
RewriteRule ^(.+)\.html$ index.php?file=$1 [NC]

Link to comment
Share on other sites

Any chance you have a $_COOKIE['test'] that only matches your root folder AND register_globals are on so that the cookie is overwriting your session variable with the same name 'test'?

 

All indications are that your $_SESSION variable is getting overwritten. Have you tried using a different name than test?

Link to comment
Share on other sites

I've echoed session_id and it remains the same at all times.

 

Only one .htaccess in the root, code below:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.co.uk$
RewriteRule ^/?$ "http\:\/\/www\.domain\.co\.uk" [R=301,L]

#Route HTML pages through index.php (page.html -> index.php?page)
RewriteRule ^(.+)\.html$ index.php?file=$1 [NC]

 

Looks to me like this rule is rewriting everything to www.....

Link to comment
Share on other sites

  • 12 years later...

I had the same issue, and in my case I had a php.ini file that wasn't being applied to the subfolders. 

I did phpinfo and you can see it there: Loaded Configuration File . Then I compared both pages, the one in the main directory, and the one in the subdirectory (where the session wasn't being loaded), and the subdirectory was using another php.ini (the default from the host).  Because in the php.ini you define the tmp folder where the session is saved. 

So then i added this to .htaccess so my custom php.ini file was accessible for the subdirectories <IfModule mod_suphp.c> suPHP_ConfigPath /home/USERNAME/PATH/TO/INI/ </IfModule>

Ok hope this helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.