I noticed that my php application behaves rather strangely on the server it runs on. When the user first visits the application and clicks on the link with an absolute path, the session data is cleared.
I recreated the problem as simple as possible. The code can be found below.
I solved this problem by removing all the absolute links in my application, I'm just looking for an explanation of this behavior .
To recreate the problem:
- click login
- click "relative link" and note that the session still has the variable "logged_in"
- click "absolute link" and note that no session data is available.
- Click the back button of the browser and note that the session data is back.
- click "absolute link" and note that the session data is again missing.
- click "home (relative link)" and note that no session data is available this time
- click "login" to reset session data
- click "absolute link" again and note that the session data has not been cleared this time
Some important points:
- This is not a problem locally on my mac MAMP runs with php 5.3.2, but it is a problem on a server with php 5.2.14 and another server running 5.3.2
- Clicking on the absolute link and then the relative home link without logging in prevents the problem from occurring after logging in.
- , , . , , .
- ( ":..." )
index.php:
<?php
session_start();
print_r($_SESSION);
?>
<br/><a href="http://www.myserver.org/page.php">Absolute link</a>
<br/><a href="page.php">Relative link</a>
<br/><a href="login.php">Log in</a> | <a href="logout.php">Log out (reset session)</a>
page.php:
<?php
session_start();
print_r($_SESSION);
?>
<br/><a href="index.php">Home (relative link)</a>
login.php:
<?php
session_start();
$_SESSION['logged_in'] = true;
header('Location: index.php');
logout.php:
<?php
session_start();
$_SESSION = array();
session_destroy();
header('Location: index.php');