Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

php - Session variables not accessible in subdirectory

When we moved our project from the beta test server to our live server we experienced the following:

Session variables are only accessible if the file accessing them is in the same folder as the file that created them. This was not the case on our beta server. So I've created three very simple test files:

test.php

<?php
session_id("581186accf44d7e80df40d0b5a47fb7d");
session_start(); 
$_SESSION['myvariable'] = 'Hello World';
?> 
<html>
    <body>
        <p><a href="page2.php">Same folder test</a></p>
        <p><a href="test/page2.php">Subfolder test</a></p>
    </body>
</html>

Then we have the following file duplicated in the same folder and in the test/ folder.

page2.php

<?php 
session_start();
print 'Here is page two, my session variable and my session cookie: '; 
print $_SESSION['myvariable']; 
print $_COOKIE['PHPSESSID'];
exit; 
?>

and here are the results: page2.php

Here is page two, my session variable and my session cookie: Hello
World581186accf44d7e80df40d0b5a47fb7d

test/page2.php

Here is page two, my session variable and my session cookie: 581186accf44d7e80df40d0b5a47fb7d

As you can see, the session variable has disappeared. But the session ID cookie is preserved in the subdirectory, so it isn't a cookie issue.

I've looked at the session block in phpinfo(); and aside from session.save_path, which is set on the beta server but not on the live server (which presumably means it will default to /tmp), the configuration is identical on both.

Also, we don't have a .htaccess file which might change domain.com paths to www.domain.com paths.

Because this test works on our beta server, I have concluded that it's a php configuration issue, but if someone could point me toward the parameter that needs changing, that would be much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, it appears that the problem occurred, in part, due to my lack of understanding of what putting my own php.ini in the public_html folder would do. Because we can't use ini_set() on our shared server (it's disabled) I created a local copy of php.ini that contained the following lines to override dud default settings...

date.timezone =  Australia/Melbourne
magic_quotes_gpc = Off

I assumed that the values in this local version of php.ini would simply override the values in the master php.ini.

Unfortunately this is not what happens on the Jumba / UberGlobal hosting. Creating this local php.ini meant that the entire configuration reverted to default values, except for these two settings.

The solution was for a tech from Jumba / Uberglobal to make a copy of their php.ini file (with just the above two values changed) and place it in our public_html folder.

This has fixed the problem.

Thanks for everyone's help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...