Note that there are some explanatory texts on larger screens.

plurals
  1. POstrange php session behavior with absolute links
    text
    copied!<p>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.</p> <p>I have recreated the problem as simply as possible. The code can be found below.</p> <p>I have solved this problem by removing all absolute links in my application, <strong>I am simply looking for an explanation of this behavior</strong>.</p> <p>To recreate the problem:</p> <ol> <li>click 'login'</li> <li>click 'relative link' and observe that the session still has the 'logged_in' variable set</li> <li>click 'absolute link' and observe that the session data appears to be missing</li> <li>click your browser's back button and observe that the session data has returned</li> <li>click 'absolute link' and observe that the session data is missing again</li> <li>click 'home (relative link)' and observe that session data is missing this time</li> <li>click 'login' to reset the session data</li> <li>click 'absolute link' again and observe that the session data was not cleared this time</li> </ol> <p>Some important things to note:</p> <ul> <li>This is not a problem locally on my mac running <a href="http://www.mamp.info/en/index.html" rel="nofollow">MAMP</a> with php 5.3.2, but is a problem on a server with php 5.2.14 and a different server running 5.3.2</li> <li>clicking the absolute link, and then the relative home link without login prevents the problem from ever occurring once you do log in.</li> <li>once the problem is solved by the method just mentioned, it can only be recreated by navigating to a different domain, clearing your browser's cache and navigating back. Clearing the cache without leaving the page will not work.</li> <li>this is also a problem if using a absolute path when redirecting using header('Location: ...')</li> </ul> <p>index.php:</p> <pre><code>&lt;?php session_start(); print_r($_SESSION); ?&gt; &lt;br/&gt;&lt;a href="http://www.myserver.org/page.php"&gt;Absolute link&lt;/a&gt; &lt;br/&gt;&lt;a href="page.php"&gt;Relative link&lt;/a&gt; &lt;br/&gt;&lt;a href="login.php"&gt;Log in&lt;/a&gt; | &lt;a href="logout.php"&gt;Log out (reset session)&lt;/a&gt; </code></pre> <p>page.php:</p> <pre><code>&lt;?php session_start(); print_r($_SESSION); ?&gt; &lt;br/&gt;&lt;a href="index.php"&gt;Home (relative link)&lt;/a&gt; </code></pre> <p>login.php:</p> <pre><code>&lt;?php session_start(); $_SESSION['logged_in'] = true; header('Location: index.php'); </code></pre> <p>logout.php:</p> <pre><code>&lt;?php session_start(); $_SESSION = array(); session_destroy(); header('Location: index.php'); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload