$_SESSION vars not setting reliably

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

$_SESSION vars not setting reliably

Post by mattcooper »

Hi all,

I'm using the following function to create part of the environment required for a simple card game:

Code: Select all

function set_Cards () {
		// Create the correct set of cards to use for the game
		// based on the value of $_SESSION['outcome']
		// CREATES THE SESSION ENVIRONMENT FOR THE FLASH...
		
		if ( $_SESSION['outcome'] == 0 ) {
		// Aaaaaaaah, they're gonna lose...
			
			$num = rand(1,3); // Generate a number to represent a card
		
			$movie = $num+1; // The outcome will be mismatched cards
			
		switch ( $num ) {  // Defines the card that the player will "choose"
		
			case 1 : $_SESSION['card'] = "jack"; break;
			case 2 : $_SESSION['card'] = "queen"; break;
			case 3 : $_SESSION['card'] = "king"; break;
			
		}
		
		switch ( $movie ) { // Defines the movie that the player will see
		
			case 1 : $_SESSION['swf'] = "queen"; break;
			case 2 : $_SESSION['swf'] = "king"; break;
			case 3 : $_SESSION['swf'] = "jack"; break;
			
		}
		} // end of first "if" statement
		
		
		if ( $_SESSION['outcome'] == 1 ) {
		// Oooooooooh, they're gonna win!!
		
			$num = rand(1,3);
			
			$movie = $num;
		
		switch ( $num ) {  // Defines the card that the player will "choose"
		
			case 1 : $_SESSION['card'] = "jack"; break;
			case 2 : $_SESSION['card'] = "queen"; break;
			case 3 : $_SESSION['card'] = "king"; break;
			
		}
		
		switch ( $movie ) { // Defines the movie that the player will see
		
			case 1 : $_SESSION['swf'] = "jack"; break;
			case 2 : $_SESSION['swf'] = "queen"; break;
			case 3 : $_SESSION['swf'] = "king"; break;
			
		}
		} // end of second "if" statement
		
	
	}
Problem is, I'm not 100% sure of the reliablity of the session vars that are being set, as sometimes the SWFs and the images for the "onClick" image swap in another part of the script aren't in the source code - it's definitely down to the session info, and is intermittent.

Also, occasionally the $_SESSION['outcome'] var is supposed to produce a "win" outcome, but (particularly with the "king" movie + card match), the outcome is "lose", which makes no sense. Have I fried my noggin' too much to be able to see a simple error in what I thought was logical randomizer code?

Thanks in advance!
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

The one glaring problem I see with your code is in the initial setting of the $movie variable, where it is assigned to the value of $num + 1. Since $num has been assigned a random value between 1 and 3, $movie can be anything between 2 and 4. Your switch($movie) statement only allows for values between 1 and 3, and no default. So you're only covering 2 of the possible outcomes.

The other issue I see is with your comparison of the value in $_SESSION['outcome'], since I don't see that set anyplace here. If the value is empty, or if it's not set altogether, $_SESSION['outcome'] == 0 may potentially be true. It's always a good idea to check with isset() first, then use a comparison.

Also, you can eliminate the second comparison, e.g. $_SESSION['outcome'] == 1, because if it's not set to 0, what other value might it be set to?

example

Code: Select all

if ( isset($_SESSION['outcome']) && $_SESSION['outcome'] == 0 ) {
    // the value is clearly 0
} else {
    // the value is not zero, it must be 1
}
Best troubleshooting method, always use var_dump() or print_r() somewhere in a receiving script to view the values stored in $_SESSION, e.g.

Code: Select all

if ( isset($_SESSION) && count($_SESSION) > 0 ) {
    echo '<pre>'; var_dump($_SESSION); echo '</pre>';
}
[ edit ] removed intval() conditional, oops ;-) [ / edit ]
Post Reply