Having a problem with session variables...

Hi guys,
Total PHP noob here, but I am trying to learn… Can any one take a look at this code and tell me what is wrong with it? Please forgive the length of the thing… This is the second of two linked pages, the first can be found at the bottom of the page… It seems to work just fine… BTW, yes, this is from a homework assignment, in it you are supposed to write a php program that lets the user input a number and for the computer to guess what the number is… Then it’s supposed to give the user a list of choices (Too High, Too Low or Correct) and the user selects one and clicks the Go button and the computer guesses again only this time with new parameters for mt_rand… I just can’t figure out what I’m doing wrong… Help, Please! Thanks in advance! ~Kannudo

[php]<?php

session_start();
mt_srand(); //seed random number generator

//checks to see if session var “alreadyDone” is TRUE and if so run nextGuess instead of firstGuess
if($_SESSION[“alreadyDone”] == TRUE){
nextGuess();
} else {
$_SESSION[“alreadyDone”] == FALSE;
firstGuess();
}

function firstGuess(){
global $num;
global $guess;
$_SESSION[“alreadyDone”] = TRUE;
$num = filter_input(INPUT_POST, “num”);
$_SESSION[‘num’] = $num;
$guess = mt_rand(1, 100);

// print out $num, $guess and give choices
print <<< HERE
Your number is $num.

I will guess $guess. Did I get it right?

 <form action = ""
       method = "post" >
 <label>Too High</label>
 <input type = "checkbox"
        name = "tooHigh" /> <br />
 <label>Too Low</label>
 <input type = "checkbox"
        name = "tooLow" /> <br />
 <label>Correct!</label>
 <input type = "checkbox"
        name = "correct" /> <br />
 <button type = "submit">Go!</button>
 </form>

HERE;
}

function nextGuess(){
global $num;
global $guess;

 //if guess was too high, make a new guess with the last guess as the max for mt_rand
 if(filter_has_var(INPUT_POST, "tooHigh")){
  $lastGuess = $guess;
  $guess = mt_rand(1, $lastGuess);
  
  //print out checkbox form again
  print <<< HERE
       "Your number is $num." <br />
       "I will guess $guess. Did I get it right?" <br />
        <form action = ""
                  method = "post" >
		 <label>Too High</label>
		 <input type = "checkbox"
			    name = "tooHigh" /> <br />
		 <label>Too Low</label>
		 <input type = "checkbox"
                            name = "tooLow" /> <br />
		 <label>Correct!</label>
		 <input type = "checkbox"
		            name = "correct" /> <br />
		 <button type = "submit">Go!</button>
		 </form>

HERE;

//if guess was too low, make a new guess with the last guess as the min for mt_rand
} elseif (filter_has_var(INPUT_POST, “tooLow”)) {
$lastGuess = $guess;
$guess = mt_rand($lastGuess, 100);

   //print out checkbox form again
   print <<< HERE
       "Your number is $num." <br />
       "I will guess $guess. Did I get it right?" <br />
        <form action = ""
                  method = "post" >
		 <label>Too High</label>
		 <input type = "checkbox"
			    name = "tooHigh" /> <br />
		 <label>Too Low</label>
		 <input type = "checkbox"
                            name = "tooLow" /> <br />
		 <label>Correct!</label>
		 <input type = "checkbox"
		            name = "correct" /> <br />
		 <button type = "submit">Go!</button>
		 </form>

HERE;

//if guess was correct, print out congrats
} else {
if(filter_has_var(INPUT_POST, “correct”)){
print “I got it right!”;
}
}
}

?>
[/php]

Here is the page that calls that one:

[php]<?php

session_start();

// prints form to get user input
print <<< HERE
Choose a number between 1 and 100 and enter it in the box below and I will try and guess what it is!

Your Number:


HERE;

?>
[/php]

i could be totally wrong, but it looks like your calling your functions before they are created.

[php]$lastGuess = $guess;[/php]

$guess is a null value here

Using globals doesn’t appear to be doing anything for you either. You should delete these from both functions:

[php]
global $num;
global $guess;
[/php]

I think what you want to do is save $guess to the session. So, in firstGuess() save to session.

[php]
$guess = mt_rand(1, 100);
$_SESSION[‘guess’] = $guess;
[/php]

Then, in nextGuess() load lastGuest value from session.

[php]
$num = $_SESSION[‘num’];
$lastGuess = $_SESSION[‘guess’];
$guess = mt_rand(1, $lastGuess);
[/php]

Note that you also want to define these before your if statement, for example:

[php]
$num = $_SESSION[‘num’];
$lastGuess = $_SESSION[‘guess’];

if (filter_has_var(INPUT_POST, “tooHigh”)) {
$guess = mt_rand(1, $lastGuess);
} else if (filter_has_var(INPUT_POST, “tooLow”)) {
$guess = mt_rand($lastGuess, 100);
} else if (filter_has_var(INPUT_POST, “correct”)) {
print “I got it right!”;
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service