Sessions

Web developer major and I’m learning about PHP sessions and hoping someone can help me troubleshoot this program. I know it’s a simple fix but I missed class and tried several ways to make this work. Once I learn this I can start a big project that’s due soon.

I’m doing the “I’m thinking of a number” in reverse so the program has to guess what number the user is thinking of in 7 guesses or less. I know exactly where the problem is and it is in bold below. Here is the code:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<title>Guess a number in reverse</title>
</head>
<body>

<h3>Pick a number between 1 and 100 and I will try to guess it!<h3>

<form method = “post” action = “”>

<?php

$_SESSION[‘high’] = isset($_SESSION[‘high’]) ? $_SESSION[‘high’] : 100;
$_SESSION[‘low’] = isset($_SESSION[‘low’]) ? $_SESSION[‘low’] : 0;
$_SESSION[‘guess’] = isset($_SESSION[‘guess’]) ? $_SESSION[‘guess’] : 0;

echo “<pre>”;
print_r($_SESSION);
echo “</pre>”;

$_SESSION[‘guess’] = rand($_SESSION[‘low’], $_SESSION[‘high’]);

print “My guess is {$_SESSION[‘guess’]}. <br />”;

//Too Low
if ($_SESSION[‘guess’] < rand($_SESSION[‘low’], $_SESSION[‘high’]))
{
$_SESSION[‘low’] = $_SESSION[‘guess’] + 1;
$_SESSION[‘guess’] = rand($_SESSION[‘guess’], $_SESSION[‘high’]);
echo “I guessed to Low. <br />”;
}

//Too High
else if ($_SESSION[‘guess’] > rand($_SESSION[‘low’], $_SESSION[‘high’]))
{
$_SESSION[‘high’] = $_SESSION[‘guess’] - 1;
$_SESSION[‘guess’] = rand($_SESSION[‘low’], $_SESSION[‘high’]);
print “I guessed too high.<br />”;
}

else {
print “I guessed right: {$_SESSION[‘guess’]} <br />”;
session_destroy();
}

print <<<HERE

<input type=“radio” name=“select” value=“correct”>Correct!<br />
<input type=“radio” name=“select” value=“high”>Too High<br />
<input type=“radio” name=“select” value=“low”>Too Low<br />

<input type=“Submit” value=“Submit”>

HERE;
?>
</form>

</body>
</html>

Worked great. The only issues were some markup. </h3>, not closed <input fields />, type=“submit” lowercase. MAKE SURE there is no space before <?php

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guess a number in reverse</title>
</head>
<body>

<h3>Pick a number between 1 and 100 and I will try to guess it!</h3>

<form method = "post" action = "">

<?php

$_SESSION['high'] = isset($_SESSION['high']) ? $_SESSION['high'] : 100;
$_SESSION['low'] = isset($_SESSION['low']) ? $_SESSION['low'] : 0;
$_SESSION['guess'] = isset($_SESSION['guess']) ? $_SESSION['guess'] : 0;

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

$_SESSION['guess'] = rand($_SESSION['low'], $_SESSION['high']);

print "My guess is {$_SESSION['guess']}. <br />";

//Too Low
if ($_SESSION['guess'] < rand($_SESSION['low'], $_SESSION['high']))
{
$_SESSION['low'] = $_SESSION['guess'] + 1;
$_SESSION['guess'] = rand($_SESSION['guess'], $_SESSION['high']);
echo "I guessed to Low. <br />";
}

//Too High
else if ($_SESSION['guess'] > rand($_SESSION['low'], $_SESSION['high']))
{
$_SESSION['high'] = $_SESSION['guess'] - 1;
$_SESSION['guess'] = rand($_SESSION['low'], $_SESSION['high']);
print "I guessed too high.<br />";
}

else {
print "I guessed right: {$_SESSION['guess']} <br />";
session_destroy();
}

print <<<HERE

<input type="radio" name="select" value="correct" />Correct!<br />
<input type="radio" name="select" value="high" />Too High<br />
<input type="radio" name="select" value="low" />Too Low<br />

<input type="submit" value="Submit" />

HERE;
?>
</form>

</body>
</html> 

I think your approach is wrong and requires a binary search.

Basically the guessed a number is halfway between the high and low limits,

if it is guess is lower then the new guess is halfway between the last guess and the lowest limit

else if higher then the new guess is halfway between the higher limit and the last guess

otherwise must be the desired number.

Have fun with you homework and best of luck with your new project :slight_smile:

Just remembered. session variables are stings so beware when trying to find the numerical mid-point of two strings.

Program is not working because it cannot guess the number in 7 guesses or less. Right now it’s just randomly spitting out numbers

On paper the below pseudo code can guess it in 7 guesses or less. I’m just having trouble putting it too work using sessions:

if result is too High
$tooHigh = $guess - 1;
$guess = round(tooLow + tooHigh)/2;
}

else if result is too low
{
$tooLow = $guess + 1;
$guess = round (tooLow + tooHigh)/2;
}

Try this:

http://www.johns-jokes.com/downloads/sp-c/session-number-guess/

Thanks for the link. That would work but can’t see the PHP to learn how it is done.

I have updated the source code and it can be seen here:

Please note that it is only a template and [B]requires ‘_guess.php’;

// file: _guess.php
[/B]


<?php 
  switch($GUESS)
  {
    case 'low':
      $_SESSION['low'] = ....
      $msg = "I guessed to Low.";
    break;  
    case 'high':
      $_SESSION['high'] =  ....
      $msg =  "I guessed too high.";
    break;  
    case 'correct':
      $msg =  "I guessed right: {$_SESSION['guess']}"; 
      session_destroy(); 
    break;  
  }//endswitch


  $_SESSION['guess'] = ....



Please post your solution.