Passing Variables without forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Passing Variables without forms

Post by SidewinderX »

Im not sure if the pseudo code shows clearly what im trying to do but ill try to explain it too

Code: Select all

<?php
if(!isset($_GET['answer'])){
	$randomNumber = rand(0,10);
	echo "Enter the value you see: <br><form id='validation' name='validation' method='get' action='test2.php'>
	  <input type='text' name='answer' />
	  <input type='submit' />
	</form>";
	echo "<br>Random Number = " . $randomNumber . "<br>";
} else {
	if($_GET['answer'] == $randomNumber){
		echo "User entered the correct number.";
		//echo "<br>Random Number = " . $randomNumber . "<br>";
	} else { 
		echo "User entered the incorrect number"; 
		//echo "<br>Random Number = " . $randomNumber . "<br>";
	}
}

?>
When the page is first loaded it generates a form and a random number. Then when the user enters something in the form and submits it, it refreshes the page, and even if the user entered the correct random number first displayed, it returns "User entered the incorrect number" because $randomNumber is null. I know I could obviousally pass the randomNumber through a hidden form field, but that would kind of defeat the whole porpose of my script. I am also aware I could just store the value in a database/text file but I dont want to do that either. Im wondering if it is possable to accomplish this using functions or arrays or something...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

Another thing

Post by timclaason »

You could have a hidden field, but have it be MD5($random), then

Code: Select all

if(md5($userinput) == $_POST['random'])
   print("All is right with the world.");
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For a solution set of 0 through 10, MD5 is darn near useless.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Either sessions, cookies, passing or database. But you should really look at the logic for this first. Closely.
Post Reply