Ok, here we go again. I am trying to use sessions to keep track of how many times a user guesses at a number. All of the code works fine except for the counter at the top. I am not aware of how to start a counting session and get it to count the number of times the user guesses. It either comes back as 2 or the value does not get passed along. (Depending on how I have my code set up at the time)Even if you point me to a resource that has an example that would be a huge help. Thanks.
<?php session_start();
$_SESSION['number'] = $_GET['number'];
$counter = $_GET['count'];
if (!isset($counter))
{
$_SESSION['count'] = 0;
}
else
{
$counter = $_SESSION['count'] + $_GET['count'];
}
?>
<!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" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--
File: guessingGame.php
Author: Nathanael Potoski
Date: 10/28/10
Description: Lets the user guess the number and shows them how many tries it took.
-->
</head>
<body>
<?php
$count = 0;
if (!isset($_SESSION['number']))
{
print "<h3>Please pick a number between 1 and 25</h3>
<form action=\"guessingGame.php\" method=\"get\">
<select name=\"number\">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
</select> <br /><br />
<input type=\"hidden\" name=\"count\" >
<input type=\"submit\" value=\"Guess\" />
</form>";
}
elseif ($_SESSION['number'] == 6)
{
print "Congratulations! You got the number! ".$_SESSION['number']."<br />";
print "You picked $counter times.<br />";
print '<form action="guessingGame.php" action="get">
<input type="hidden" name=\"finish\" value="True">
<input type="submit" value="Finish"';
}
elseif ($session['number'] != 6)
{
print "Sorry, please pick again.";
print "<h3>Please pick a number between 1 and 25</h3>
<form action=\"guessingGame.php\" method=\"get\">
<select name=\"number\">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
</select> <br /><br />
<input type=\"hidden\" name=\"count\" >
<input type=\"submit\" value=\"Guess\" />
</form>";
}
?>
</body>
</html>