I have observed my php application behaving rather strangely on the server that it is running on. When a user first visits the application, and clicks on a link with an absolute path, the session data is cleared.
I have recreated the problem as simply as possible. The code can be found below.
I have solved this problem by removing all absolute links in my application, I am simply looking for an explanation of this behavior.
To recreate the problem:
Some important things to note:
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');
At least in your example the pages are switching between two domains (rhun.ithaca.edu and www.ithacahealth.org). You'll notice that if you click "Log in" on both domains, then you'll have logged_in=1 in all cases. Anyway, that's the primary cause of the problem - two different domains.
Session cookies does not differ from any other cookies (from a browser's point of view), so they are subject to the same limitations - the relevant one being that you have to be on the same domain. You can change the session cookie settings with session_set_cookie_params() (that has to be done before session_start(), but even so you cannot allow the same cookie to be read from a different domain, only from a subdomain, if you require it.
Also, I don't know if it is relevant, but keeping the webpage on a singe domain/subdomain might help a little with search engine optimization - especially in cases where there is different content between the domains/subdomains, search engines might consider them to be different webpages and split their pagerank between them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With