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

FairyBusinessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FairyBusinessAuthor Commented:
Well, I want them to start at 0 then increase as the game goes on. . .
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.