0

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>
3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by cwarn23
0

did you try this changing this code

if (!isset($counter))
      {
      	$_SESSION['count'] = 0;
      }
      else
      {
      	$counter = $_SESSION['count'] + $_GET['count'];
      }

to this

if (!isset($counter))
      {
      	$_SESSION[$counter] = 0;
      }
      else
      {
      	$counter = $_SESSION[$counter] + $_GET[ $counter];
      }
0

No, you need the single quotes inside to denote that those are values that you are receiving. Without them you just have gibberish that php doesn't understand. Thanks, though.

0

Try the following:

<?php session_start();
      $_SESSION['number'] = $_GET['number'];
      if (!isset($_SESSION['count']))
      {
      	$_SESSION['count'] = 0;
      }
      else
      {
      	$_SESSION['count']++;
      }
$counter = $_SESSION['count'];

But be sure to delete cookies before testing so you have a fresh start and new sessions.

0

Ok, that got my counting done. Thank you tons! But now I am having a problem clearing my session. What they give me in the book is FAR from clear for a self processing file. Here is what I have in my session section:

<?php session_start();
      
      if(isset($_GET['finish']))
      {
      	unset($_SESSION);
      	session_destroy();
      }
      else
      {
         $_SESSION['number'] = $_GET['number'];
      
         if (!isset($_SESSION['count']))
         {
      	   $_SESSION['count'] = 0;
         }
         else
         {
      	   $_SESSION['count'] ++ ;
         }
         $counter = $_SESSION['count'];
      

?>

And here is what is supposed to be sending the clear signal to the first IF statement:

print "Congratulations! You got the number! ".$_SESSION['number']."<br />";
    	print "You picked $counter times.<br />";
    	print '<form action="guessingGame.php" action="get">
    	      <input type="submit" value="Finish"
    	      <input type="hidden" name=\"finish\" value="true">';

So the question is, what am I doing wrong? Am I calling functions out of order?

0

Try this:

<?php session_start();
 
      if(isset($_GET['finish']))
      {
      	$_SESSION=array();
      }
      else
      {
         $_SESSION['number'] = $_GET['number'];
 
         if (!isset($_SESSION['count']))
         {
      	   $_SESSION['count'] = 0;
         }
         else
         {
      	   $_SESSION['count'] ++ ;
         }
         $counter = $_SESSION['count'];
 
 
?>
0

ok, I got the counting to work, but for some reason, no matter how I arrange the code or what I put in the code, it will not erase the session. =(

<?php session_start();

      
      if($_GET['finish'] == 'true')
      {
      	$_SESSION = array();
      	session_destroy();
      	setcookie('PHPSESSID', '', time()-300);
      }
      else
      {
        $number = $_GET['number'];
      
         if (!isset($_SESSION['count']))
         {
      	   $_SESSION['count'] = 0;
         }
         else
         {
      	   $_SESSION['count'] ++ ;
         }
       if(!isset($_SESSION['rand']))
       {
       	   $_SESSION['rand'] = rand(1,25);
       } 
      

?>
0

Try adding this to your html form and your code should then work.

<input type="hidden" name="finish" value="true">
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.