TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-14-2012, 04:35 PM   #1 (permalink)
bgs
The Visitor
 
Join Date: Feb 2012
Posts: 2
Thanks: 0
bgs is on a distinguished road
Default Creating a random number once and keeping it

Hi,

I'm just beginning to learn PHP (day 2) and I can't really seem to figure this one out. My idea is a simple number guessing game that works something like this:

$num = rand(0,100);

(Accept $guess from user input)

if $num < $guess echo 'too low' etc.

The form references itself and sends the guess via POST. My problem is that the random number is getting set each time. I come from a forms/console development background and this is the first time I've delved into web programming, so there's some element here regarding the 'session' that isn't quite straight in my head.

Please feel free to enlighten me, poke fun at my code, etc.! Thanks for your help.

This is my code. It's almost entirely lifted from "Sams Teach Yourself PHP, MySQL and Apache" but I've modified the functionality to attempt to use a random number.

Code:
<?php
	$num = rand(0,100);
	if (!isset($_POST["guess"]))
	{
		$message = "What number am I thinking of?";
	}
	else if ($_POST["guess"] > $num)
	{
		$message = "Too high, guess again!";
	}
	else if ($_POST["guess"] < $num)
	{
		$message = "Too low, guess again!";
	}
	else
	{
		$message = "Correct!";
	}
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess" />
<p><input type="submit" value="Submit"/></p>
</form>
<?php echo $num ?> <!-- just so I can see the output -->
</body>
</html>
bgs is offline  
Reply With Quote
Old 02-14-2012, 05:03 PM   #2 (permalink)
bgs
The Visitor
 
Join Date: Feb 2012
Posts: 2
Thanks: 0
bgs is on a distinguished road
Default

Well, I've fixed it by using a hidden input and passing the value along via POST. Here's the working code:

Code:
<?php

	if (!isset($_POST["guess"]))
	{
		$message = "What number am I thinking of?";
		$num = rand(0,100);
	}
	else
	{
		$num = $_POST['num'];
		if ($_POST["guess"] > $num)
		{
			$message = "Too high, guess again!";
		}
		else if ($_POST["guess"] < $num)
		{
			$message = "Too low, guess again!";
		}
		else
		{
			$message = "Correct!";
		}
}
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess"/>
<input type="hidden" name="num" value="<?php echo $num ?>" />
<p><input type="submit" value="Submit"/></p>
</form>
<?php echo $num ?>
</body>
</html>
bgs is offline  
Reply With Quote
Old 02-15-2012, 04:50 AM   #3 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

The thing about web development is that it is stateless due to the nature of http requests. Meaning that you can't save state between requests (for example when you submit a form or click on a link). There is a few things you can do here to save the random number between requests, you can use a cookie to save it and then obtain it later, another would be using sessions (which sometimes are implemented using cookies). The cookies are saved on the client while the sessions are most of the time saved in client and sometimes in server. But on the server side you can also save it in a database or a local file.

Or you can also use the hidden input tag to keep it in the between requests.

The next is an example of how it would look using the hidden input. Just gotta make sure the

PHP Code:
<?php
$message = "What number am I thinking of?";
$num = $_POST['num'] ? $_POST['num'] : rand(0,100);
if (isset($_POST["submit"])) {
  $num = $_POST['num'];
  if ($_POST['guess'] > $num) {
    $message = "Too high, guess again!";
  } elseif ($_POST['guess'] < $num) {
    $message = "Too low, guess again!";
  } else {
    $message = "Correct!";
  }
}
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess"/>
<input type="hidden" name="num" value="<?php echo $num ?>" />
<p><input type="submit" name="submit" value="Submit"/></p>
</form>
<?php echo isset($_POST['submit']) ? $num : ''; ?>
</body>
</html>
tony is offline  
Reply With Quote
Old 04-01-2012, 08:37 AM   #4 (permalink)
The Visitor
 
Join Date: Apr 2012
Posts: 1
Thanks: 0
jojoone is on a distinguished road
Default

Even though it's correct which you ought to educate oneself in affiliate advertising, the best sources Karen Millen Dresses are not programs. However, books are.Karen Millen Coats
Karen Millen One Shoulder Dress By basically studying up on affiliate advertising, you'll be able to get fantastic insights and preserve lots of funds. By simply looking on retail sites such as Amazon or EBay, it is possible to get hundred of titles on the subject.Karen
Millen Solid Color Dresses
You can find lots of step-by-step guides obtainable on the way to get your affiliate
advertising and marketing business going and the way to develop it up.
One more great useful resource for affiliate advertising may be the web. Merely by typing affiliate advertising and
marketing into any major online search engine, lots of info arrives up. You will find lots of free classes online that you
could take around the subject in the event you are established. Read by means of the information readily available for you for absolutely nothing.Karen Millen Multicolor Dresses Karen Millen Skirts Chances really are a course or a seminar will not educate you anymore than what you are able to learn on the web at no cost.
jojoone is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:39 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design