Jump to content

[SOLVED] Retaining a random, encrypted value on page process


Goldeneye

Recommended Posts

So as part of my anti-spam system, I have this randomly generated/encrypted string which the user has to input into a field called 'key'. I assigned the function that generates this values, a variable called '$lock' which I define at the top of the script.

 

Now, when I process the form, the value gets generated again, causing the returned error message.

 

So my question is, how do I retain the value that was initially generated by the randStr() function.

<?php //import.php snippet
function randStr(){
	$str = md5(uniqid(mt_rand(), true));
	return $str;
}
?>
<?php //The register script
require('import.php');

$lock = randStr();

if (isset($_POST['action'])) {
	if(empty($_POST['key']) || $_POST['key'] !== $lock){ //The Anti-Spam field was empty / doesn't match $lock
		die('The anti-spam field was either: empty - you completely disregarded it; or did not match the lock');
	} else {
		// Process the form
	}
} else {
	echo '<form name="register" action="register.php" method="post">';
	echo '<label for="pseudonym1" class="userRegistration">Pseudonym:</label> <input type="text" class="textfield" name="pseudonym" value="' . stripslashes($_POST['pseudonym']) . '" id="pseudonym1"><br>';
	echo '<label for="passkey1" class="userRegistration">Passkey:</label> <input type="password" class="textfield" name="passkey" value="' . stripslashes($_POST['passkey']) . '" id="passkey1"><br>';
	echo '<label for="email1" class="userRegistration">Email:</label> <input type="text" class="textfield" name="email" value="' . stripslashes($_POST['email']) . '" id="email1"><br>';
	echo 'Anti-Spam:<input type="text" class="textfield" name="key"> ' . $key . '';
	echo '<label for="takeaction" class="userRegistration"> </label> <input type="submit" class="button" name="action" value="Register" id="takeaction">';
	echo '</form>';
}
gFooter();
?>

The simplest way would be to use sessions.

 

session_start();
if (empty($_SESSION['lock'])) {
  $lock = randStr();
  $_SESSION['lock'] = $lock;
}

 

Then $_SESSION['lock'] will retain its value throughout multiple calls to your script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.