Page 1 of 1

Passing Variables without forms

Posted: Sat Dec 02, 2006 5:27 pm
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...

Posted: Sat Dec 02, 2006 5:31 pm
by feyd

Another thing

Posted: Mon Dec 04, 2006 2:58 pm
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.");

Posted: Mon Dec 04, 2006 5:07 pm
by feyd
For a solution set of 0 through 10, MD5 is darn near useless.

Posted: Mon Dec 04, 2006 6:38 pm
by RobertGonzalez
Either sessions, cookies, passing or database. But you should really look at the logic for this first. Closely.