Under a specific condition, session variables declared in templates exhibit unexpected behavior
|Reported by:||dougwaltman||Owned by:|
I believe I have discovered a very peculiar bug that occurred when I tried to set a single session variable in a template file, "header.php", and call it again after refreshing the page. I have written a quick snippet of code that can be used to replicate the issue. This code will:
- Start the session
- Display some HTML
- Create an array of values
- Shuffle the values
- Display the first value as "new"
- Display the session variable of the last stored value
- Set the session variable to the "new" one
Add the following code to the top of "header.php" in any template:
<?php session_start(); ?> <link rel="icon" href="<? bloginfo('template_url') ?>/images/x" /> <?php $myarray = array( 'a','b','c','d','e' ); shuffle($myarray); echo "<pre>New: ".$myarray; echo "\nOld: ".$_SESSION['last_one_displayed']; $_SESSION['last_one_displayed'] = $myarray; exit; ?>
After refreshing the page several times, you will notice that the value for "old" isn't matching the "new" value from the previous refresh. The code will behave as expected if you do any number of things, including:
- Change 'rel="icon"' to anything else, suchas 'rel="icons"' or 'rely="icon"'
- Remove the 'rel="icon"' attribute entirely
- Remove the 'x' from the end of the url
- Remove the '<? bloginfo('template_url') ?>' snippet
- Move the '<link...' line below the rest of the code
I suspect that this bug has something to do with the code that compiles and displays the template.