- Impact
- 19
Hey
as i said in a previous thread, i am learning php and i am trying to make a "Guess the number" game..but everytime the browser refreshes/user inputs a number ..the random number changes..i am using sessions but it still doesnt work..can anyone help?
heres the code:
as i said in a previous thread, i am learning php and i am trying to make a "Guess the number" game..but everytime the browser refreshes/user inputs a number ..the random number changes..i am using sessions but it still doesnt work..can anyone help?
heres the code:
PHP:
<?php
session_start();
$user_guess = (int)$_POST[guess];
if (empty($_Session['number'])) {
$_Session['number'] = rand(1,100);
}
$num_to_guess = $_Session['number'];
$message ="";
$counter = 0;
if (!isset($user_guess)){
$message="Welcome to the guessing machine!";
}
else if ($user_guess > $num_to_guess) {
$message = "$user_guess is too big! try a smaller number";
$counter++;
}
else if ($user_guess < $num_to_guess) {
$message = "$user_guess is too small! try a bigger number";
$counter++;
}
else {
$message = "Well done! it tool you $counter times to get the right number";
}
?>
<html>
<head>
<title>Number</title</head>
<body>
<p><h1><?php echo $message; ?> </h1></p>
<form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="POST">
<p>Guess: <input type="text" name="guess"></p>
</p><input type="submit" value="submit your guesS"></p>
</form>
</body>
</html>







