| 
<?php
//start the sesson
 session_start();
 //include the class
 include('rndkvp.class.php');
 //instnatiate the class or load from saved object
 if( empty($_SESSION['humanCheck']) ){
 $humanCheck = new rndkvp(10);
 }else{
 $humanCheck = unserialize($_SESSION['humanCheck']);
 }
 
 //once form is submitted
 if( !empty($_REQUEST['form-submitted']) ){
 
 //load request variables into system variables
 $formName = $_REQUEST['form-name'];
 $formComment = $_REQUEST['form-comment'];
 //force fail if the key is not found in the request
 $value = ( empty($_REQUEST[$humanCheck->key]) ) ? 'fail' : $_REQUEST[$humanCheck->key];
 
 //test system variables for errors
 $error = '';
 if( empty( $formName ) ){
 $error .= 'Please provide a Name<br>';
 }
 if( empty( $formComment ) ){
 $error .= 'Please provide a Comment<br>';
 }
 
 //if no errors found
 if( empty($error) ){
 //validate value submitted
 if( $humanCheck->validate( $value ) != true ){
 die('Failed human validation<br><a href="">Try Again</a>');
 }
 //validation passed, reset key value pairs
 $humanCheck->resetKVP();
 $formComment = 'Passed Human Validation'."\n";
 $formComment .= 'Testing key: '.$humanCheck->key.' and value: '.$humanCheck->value;
 }
 
 }else{
 //set up form defaults
 $formName = '';
 $formComment = 'Testing key: '.$humanCheck->key.' and value: '.$humanCheck->value;
 
 }
 
 //save the object to the session
 $_SESSION['humanCheck'] = serialize($humanCheck);
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 <title>Random Key Value Pair Testing</title>
 <meta charset="UTF-8">
 </head>
 <body>
 <form method="POST">
 <?php
 if( !empty($error) ){
 ?>
 <div style="border: thin solid red;"><?php echo $error;?></div>
 <?php
 }
 ?>
 <div>Name: <input type="text" name="form-name" value="<?php echo $formName;?>"></div>
 <div>Comment:<br><textarea name="form-comment" rows="10" cols="60"><?php echo $formComment;?></textarea></div>
 <div>
 <input type="hidden" name="form-submitted" value="1">
 <?php echo $humanCheck->createFormInput();?>
 <input type="submit" name="form-submit" value="Submit">
 </div>
 </form>
 <hr>
 <h4>Testing Process</h4>
 <p>1: Submit the form without entering a name. You should see an error and the key value pairs remain the same.</p>
 <p>2: Submit the form after entering a name. You should see the success and the key value pairs change. Continue to submit a few more times and see the key value pairs change.</p>
 <p>3: Refresh the page to simulate a form submission by a bot. You may need to accept the refresh warning. Validation should fail.</p>
 </body>
 </html>
 
 |