NameSilo

$_Session help

SpaceshipSpaceship
SpaceshipSpaceship
NamecheapNamecheap
Watch
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:
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>
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Try changing them from $_Session to $_SESSION
 
0
Close
lol yup that was the problem :p
Thanks :)
 
0
Close
On an unrelated note, you forgot to close your <title> tag properly.
 
0
Close
Another unrelated note: "Well done! it tool you $counter times to get the right number";
 
0
Close
Another unrelated note: you close a <p> tag before it's opened.
Code:
</p><input type="submit" value="submit your guesS"></p>

Eh, I had nothing to do. Fixed.
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 took you $counter times to get the right number";
     }

?>        
<html>
<head>
<title>Number</title></head>
<body>
<p><h1><?=$message?> </h1></p>
<form action="?" method="POST">
<p>Guess: <input type="text" name="guess"></p>
<p><input type="submit" value="Submit Your Guess"></p>
</form>
</body>
</html>
 
Last edited:
0
Close
Another unrelated note: your input's aren't valid XHTML. (That's the best I could do.. I think that's it.)

edit: I got another one if Tree is done. ;)

edit2: It seems like he is, so:
Another unrelated note: $counter will always be 0 when the person finishes. You need to make that a session variable and increment it when its wrong and not set it to 0 every time unless it's not set.
 
Last edited:
0
Close
lol thanks for the notes guyz..its just a fun test game and i m no where close 2 done:p anyway i got another small problem
it never shows this message:

$message="Welcome to the guessing machine!";
 
0
Close
$user_guess = (int)$_POST[guess];
if (!isset($user_guess)){

You set it.. you need to check if it is empty.
 
0
Close
okay awesome:) heres the new code
p.s: dan: whats xhtml valid form thing u were tellign me about?
PHP:
<?php
	session_start(); // Starts the session
	if (empty($_SESSION['number'])) { 
		$_SESSION['number'] = rand(1,100);
	}

	if(empty($counter)) {
		$_SESSION['count'] = 1;
	}

	# Variables
	$num_to_guess = $_SESSION['number']; // Random number
	$message =""; 
	$user_guess = (int)$_POST[guess];
	$counter=$_SESSION['count'];
	
	if (empty($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 took you ".++$counter." times to get the right number";
		unset($_SESSION['number']);
		unset($_SESSION['Count']);
     	}

?>
        
<html>
<head>
	<title>Number</title>
</head>
<body>

	<p><h1><?=$message?> </h1></p>
	<form action="?" method="POST">
		<p>Guess: <input type="text" name="guess"></p>
		<p><input type="submit" value="Submit Your Guess"></p>
	</form>

</body>
</html>
however, now the counter has problems..it alwayz display 2 for the counter
 
0
Close
PHP:
<?php

session_start(); // Starts the session
if (empty($_SESSION['number'])) { 
	$_SESSION['number'] = rand(1,100);
}

if (empty($_SESSION['count'])) {
	$_SESSION['count'] = 1;
} else {
	if (!empty($_POST['guess'])) $_SESSION['count']++; 
}

$num_to_guess = $_SESSION['number']; // Random number
$message      = ""; 
$user_guess   = (int)$_POST[guess];
$counter      = $_SESSION['count'];

if (empty($user_guess)){
	$message="Welcome to the guessing machine!";
} elseif ($user_guess > $num_to_guess) {
	$message = "$user_guess is too big! Try a smaller number";
	$counter++;
} elseif ($user_guess < $num_to_guess) {
	$message = "$user_guess is too small! Try a bigger number";
	$counter++;
} else {
	$message = "Well done! It took you ".--$counter." times to get the right number";
	unset($_SESSION['number']);
	unset($_SESSION['count']);
}

?>
        
<html>
<head>
    <title>Number</title>
</head>
<body>
    <p><h1><?=$message?></h1></p>
    <form action="?" method="POST">
        <p>Guess: <input type="text" name="guess" /></p>
        <p><input type="submit" value="Submit Your Guess" /></p>
    </form>
</body>
</html>
You just need to watch variable names and use the correct case. Other than a few errors like counter instead of count, the script would work good.
 
0
Close
hey
thanks dan :)
i got another problem though..if user inputs 0 it shows them the "Welcome" message
i dont know why it does it..i guess 0 = empty? how do i fix that
 
0
Close
Try this:
Modified the code a bit...
PHP:
<?php 

session_start(); // Starts the session 
$message      = "";  
$counter      = $_SESSION['count']; 
if (!isset($_SESSION['done'])) {
   $message="Welcome to the guessing machine!";
}
else if ($_SESSION['done'] = 1) {
if ($_POST[guess] = 0) {
    $message = "$user_guess is too small! Try a bigger number"; 
    $counter++;
}
if (empty($_SESSION['number'])) {  
    $_SESSION['number'] = rand(1,100); 
} 

if (empty($_SESSION['count'])) { 
    $_SESSION['count'] = 1; 
} else { 
    if (!empty($_POST['guess'])) $_SESSION['count']++;  
} 

$num_to_guess = $_SESSION['number']; // Random number 
$user_guess   = (int)$_POST[guess]; 

if (empty($user_guess)){ 
    $message="Welcome to the guessing machine!"; 
} elseif ($user_guess > $num_to_guess) { 
    $message = "$user_guess is too big! Try a smaller number"; 
    $counter++; 
} elseif ($user_guess < $num_to_guess) { 
    $message = "$user_guess is too small! Try a bigger number"; 
    $counter++; 
} else { 
    $message = "Well done! It took you ".--$counter." times to get the right number"; 
    unset($_SESSION['number']); 
    unset($_SESSION['count']); 
} 
}

?> 
         
<html> 
<head> 
    <title>Number</title> 
</head> 
<body> 
    <p><h1><?=$message?></h1></p> 
    <form action="?" method="POST"> 
        <p>Guess: <input type="text" name="guess" value="<?PHP ECHO $_POST[guess]; ?>"/></p> 
        <p><input type="submit" value="Submit Your Guess" /></p> 
        <input type="hidden" name="done" value="1" />
    </form> 
</body> 
</html>
 
0
Close
now it alwayz shows "Welcome to the guessing machine"
 
0
Close
sukantab, what's the point of $_SESSION['done'];? I don't really see what any of that does other than add an if statement.

unknowngiver, the code I posted worked fine and did everything you wanted it to, right?
 
0
Close
Dan: yes but if you enter "0" it gives the welcome message again..instead of saying "number is 2 small"..
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back