• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Why won't my sessions work?

Hello, my code seems to be working except that it does not store the data in the sessions, so its not remembering what the last record was.

Can someone tell me why my sessions aren't working?
<?php session_start() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<h1>Rock, Paper, Scissors</h1>
<form action="" method = "post">
  <fieldset>
  <button type="submit" name="userGuess" value="0">rock</button>

  <button type="submit" name="userGuess" value="1">paper</button>

  <button type ="submit" name="userGuess" value="2">scissors</button>
  </fieldset>
</form>

<?php

$devices = array("rock", "paper", "scissors");

$combos = array(
  array("Tie", "Computer wins", "Human wins"),
  array("Human wins", "Tie", "Computer wins"),
  array("Computers wins", "Human wins", "Tie"),
);

$human = filter_input(INPUT_POST, "userGuess");
$deviceHuman;

$computer = rand(0, 2); // Give the computer a random value
$deviceComputer = $devices[$computer]; // What the computer got

$outcome;

// Number of wins per each
$_SESSION['humanWins'] = 0;
$_SESSION['computerWins'] = 0;
$_SESSION['ties'] = 0;

// The percentages for total wins per each
$_SESSION['humanPercent'] = 0;
$_SESSION['computerPercent'] = 0;
$_SESSION['tiesPercent'] = 0;

if(isset($human)) { // If the game has already been played
	play();
}
else { // If it's never been played then display default message
	display();	
}

function display() {
	print "<h2>Welcome to Rock, Paper, Scissors!</h2>";	
}

function play() {
	global $human, $computer, $combos, $deviceHuman, $deviceComputer, $devices, $outcome, $humanWins, $computerWins, $ties, $humanPercent, $computerPercent, $tiesPercent, $total;
	
	$deviceHuman = $devices[$human]; //What the user selected
	$outcome = $combos[$human][$computer];  //Decides who wins

	print "Human: " . $deviceHuman;
	print "<p> Computer: " . $deviceComputer;
	print "<p> Outcome: " . $outcome . "<p>";

	if($outcome == "Human wins") {
		$humanWins++;		
		$humanPercent = ($humanWins/$total) * 100;
	}
	else if($outcome == "Computer wins") {
		$computerWins++;	
		$computerPercent = ($computerWins/$total) * 100;
	}
	else if($outcome == "Tie") {
		$ties++;	
		$tiesPercent = ($ties/$total) * 100;
	}
	// The total number of wins
	$_SESSION['total'] = $humanWins + $computerWins + $ties;
		
	print "Human wins: " . $humanWins . " (" . $humanPercent . "%)<p>";
	print "Computer wins: " . $computerWins . " (" . $computerPercent . "%)<p>";
	print "Ties: " . $ties . " (" . $tiesPercent . "%)<p>";
}

?>

</body>
</html>

Open in new window

0
FairyBusiness
Asked:
FairyBusiness
  • 4
  • 3
1 Solution
 
ropennerCommented:
You could not set them to zero if the session already exists.

line 40 above:

if(!isset($_SESSION['humanWins'])) {
  // Number of wins per each
  $_SESSION['humanWins'] = 0;
  $_SESSION['computerWins'] = 0;
  $_SESSION['ties'] = 0;

  // The percentages for total wins per each
  $_SESSION['humanPercent'] = 0;
  $_SESSION['computerPercent'] = 0;
  $_SESSION['tiesPercent'] = 0;
}

?>
0
 
FairyBusinessAuthor Commented:
Well, I want them to start at 0 then increase as the game goes on. . .
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
FairyBusinessAuthor Commented:
Ok, I think I get it now. Thanks
0
 
ropennerCommented:
$computerPercent is not the same as $_SESSION['computerPercent'] so when incrementing you need to use the session variables.  I think this is your intent.
0
 
Lukasz ChmielewskiCommented:
I might miss something, but try below code snippet - you need to set the session values to 0 at the beginning of the game

<?php session_start();

if(empty($_SESSION['humanWins'])){$_SESSION['humanWins'] = 0;}
if(empty($_SESSION['computerWins'])){$_SESSION['computerWins'] = 0;}
if(empty($_SESSION['ties'])){$_SESSION['ties'] = 0;}

if(empty($_SESSION['humanPercent'])){$_SESSION['humanPercent'] = 0;}
if(empty($_SESSION['computerPercent'])){$_SESSION['computerPercent'] = 0;}
if(empty($_SESSION['tiesPercent'])){$_SESSION['tiesPercent'] = 0;}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<h1>Rock, Paper, Scissors</h1>
<form action="" method = "post">
  <fieldset>
  <button type="submit" name="userGuess" value="0">rock</button>

  <button type="submit" name="userGuess" value="1">paper</button>

  <button type ="submit" name="userGuess" value="2">scissors</button>
  </fieldset>
</form>

<?php

$devices = array("rock", "paper", "scissors");

$combos = array(
  array("Tie", "Computer wins", "Human wins"),
  array("Human wins", "Tie", "Computer wins"),
  array("Computers wins", "Human wins", "Tie"),
);

$human = filter_input(INPUT_POST, "userGuess");
$deviceHuman;

$computer = rand(0, 2); // Give the computer a random value
$deviceComputer = $devices[$computer]; // What the computer got

$outcome;



if(isset($human)) { // If the game has already been played
	play();
}
else { // If it's never been played then display default message
	display();	
}

function display() {
	print "<h2>Welcome to Rock, Paper, Scissors!</h2>";	
}

function play() {
	global $human, $computer, $combos, $deviceHuman, $deviceComputer, $devices, $outcome, $humanWins, $computerWins, $ties, $humanPercent, $computerPercent, $tiesPercent, $total;
	
	$deviceHuman = $devices[$human]; //What the user selected
	$outcome = $combos[$human][$computer];  //Decides who wins

	print "Human: " . $deviceHuman;
	print "<p> Computer: " . $deviceComputer;
	print "<p> Outcome: " . $outcome . "<p>";

	if($outcome == "Human wins") {
		$_SESSION['humanWins']++;		
		$humanPercent = ($_SESSION['humanWins']/$total) * 100;
	}
	else if($outcome == "Computer wins") {
		$_SESSION['computerWins']++;	
		$computerPercent = ($_SESSION['computerWins']/$total) * 100;
	}
	else if($outcome == "Tie") {
		$_SESSION['ties']++;	
		$tiesPercent = ($_SESSION['ties']/$total) * 100;
	}
	// The total number of wins
	$_SESSION['total'] = $_SESSION['humanWins'] + $_SESSION['computerWins'] + $_SESSION['ties'];
		
	print "Human wins: " . $_SESSION['humanWins'] . " (" . $_SESSION['humanPercent'] . "%)<p>";
	print "Computer wins: " . $_SESSION['computerWins'] . " (" . $_SESSION['computerPercent'] . "%)<p>";
	print "Ties: " . $_SESSION['ties'] . " (" . $_SESSION['tiesPercent'] . "%)<p>";
}

?>

</body>
</html>

Open in new window

0
 
FairyBusinessAuthor Commented:
I didnt know that.

if I do $computerPercent = $_SESSION['computerPercent']

then I can use $computerPercent from then on??
0
 
ropennerCommented:
yes
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now