WordPress.org

Ready to get started?Download WordPress

Forums

Bug? Session data not being saved correctly? (2 posts)

  1. aritrea
    Member
    Posted 4 months ago #

    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>
  2. aritrea
    Member
    Posted 4 months ago #

    reading my description, it seems to be a little bit too complicated ;)

    Here is a much shorter description of the Problem:

    $_SESSION is unset even if the corresponding call to do this is in the false-part, thus should never be called

    updated code snippet:

    <?php
    session_start();
    
    function test() {
    	// return true; // this works, too!!
    	$val = (isset($_GET['test']) && $_GET['test'] == "test");
    	return $val;
    }
    
    ?>
    <!DOCTYPE html>
    <html>
    	<head>
    		<?php wp_head(); ?> <?php // removing this && next line => works in firefox ?>
    		<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"> <?php // removing this line => works in chrome ?>
    	</head>
    	<body style="text-align:center; ">
    		<div>
    			<h1>
    				Session before
    			</h1>
    			<div>
    				<?php
    				var_dump($_SESSION);
    				?>
    			</div>
    		</div>
    		<div>
    			<?php
    			if (test()) {
    				unset( $_SESSION['should_not_be_set'] );
    				$_SESSION['identifier'] = md5(microtime());
    				echo "<br />reload page to see if SESSION changes";
    			} else {
    				$_SESSION['should_not_be_set'] = date('H:i:s');
    				echo "<a href='?test=test'>CLICK ME TO BEGIN</a>";
    			}
    
    			?>
    		</div>
    		<h1>
    			Session after
    		</h1>
    		<div>
    			<?php
    			var_dump($_SESSION);
    			?>
    		</div>
    		<?php wp_footer(); ?>
    	</body>
    </html>

    call: index.php?test=test

Reply

You must log in to post.

About this Topic

Tags

No tags yet.