Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

aritrea on "Bug? Session data not being saved correctly?"

$
0
0

hi there,

I am using latest wordpress 3.8.1 to deploy a site and I use SESSION variables to store some information. Anyway, I get a really strange behaviour which I could reduce to the attached code snippet below.

let me describe what I want to do:
- depending on a test() function, i want to set a session variable with a random number
- if the test function fails (returns false), all session data shall be unset by using session_unset()
- by refreshing the page (thus the test-function returns true again), another pseudo-random number should be generated and saved to the session.
- a var_dump of my session just before should therefore display the OLD random number which has just been set

what I get:
- it does NOT display the old random number, but unsets the whole session array

the following steps make it work as expected, but - of course - are not an option with my website:
- removing wordpress related stuff like wp_head and so on to make it look like a usual non-wordpress php site
- simply returning "true" in my test function (anyway, var_dump of this function also returns true)
- uncomment session_unset() - line
- remove bloginf() call => works with chrome on mac (does NOT work with firefox on mac?!?!)
- remove bloginfo() AND wp_head() call => works with firefox as well

another strange thing: removing some lines work with chrome, but not with firefox. its quite late, so I might just miss a huge thing here, but how does the browser affect php's execution?!?!

I'm really confused and dont know how to fix this issue..
Anyway, here is the code ive been talking of. feel free to play with it.

best regards,
Christoph

<?php
session_start();

function test() {
	// return true; // this works, too!!
	$val = (isset($_GET['action']) && $_GET['action'] == "support");
	return $val;
}

?>
<!DOCTYPE html>
<html>
	<head>
		<?php
		// Chrome: remove bloginfo()
		// Firefox: remove wp_head() && bloginfo()
		?>
		<?php //*  ?>
		<?php wp_head(); ?>
		<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>">
		<?php // */ ?>
	</head>
	<body>
		<div style="text-align: center">
			<h1>
				Session
			</h1>
			<div>
				<?php
				var_dump($_SESSION);
				?>
			</div>
		</div>
		<div id="main" class="content-area">
			<div style="margin: 20px; text-align:center; font-weight:bold; font-size: 200%;">
				<?php
				if (test()) {
					$_SESSION['identifier'] = md5(microtime()); // set an identifier

					if (!empty($_SESSION['identifier']))
						echo "<br />new identifier: ".$_SESSION['identifier'];

					echo "<br />reload page to see if SESSION changes";
				} else {
					session_unset(); // uncomment this line => it works ?! (reload twice)
					echo "<a href='?action=support'>CLICK ME TO BEGIN</a>";
				}

				?>
			<?php wp_footer(); ?>
			</div> <!-- .container -->
		</div> <!-- #main -->
	</body>
</html>

Viewing all articles
Browse latest Browse all 8245

Trending Articles