Goldeneye Posted July 29, 2008 Share Posted July 29, 2008 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(); ?> Link to comment https://forums.phpfreaks.com/topic/117083-solved-retaining-a-random-encrypted-value-on-page-process/ Share on other sites More sharing options...
btherl Posted July 29, 2008 Share Posted July 29, 2008 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. Link to comment https://forums.phpfreaks.com/topic/117083-solved-retaining-a-random-encrypted-value-on-page-process/#findComment-602210 Share on other sites More sharing options...
Goldeneye Posted July 29, 2008 Author Share Posted July 29, 2008 Ahhh of course, then I could simply unset it once the script has successfully processed. Well I completely overlooked that, anyways, thanks a lot! Link to comment https://forums.phpfreaks.com/topic/117083-solved-retaining-a-random-encrypted-value-on-page-process/#findComment-602217 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.