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>