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

Any .htaccess files in either or both the admin and root folder that could be affecting the session settings?

 

I recommend that you echo session_id(); after the session_start() statements so that you can see if/when the session id is changing.

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.