PHP Classes

Random key value pair authentication: Authenticate human users using random value pairs

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 172 All time: 8,817 This week: 660Up
Version License PHP version Categories
rndkvp 1.0.0Public Domain5.6PHP 5, User Management, Validation, S...
Description 

Author

This class can authenticate human users using random value pairs.

It can generate two random strings that will be used as key-value pairs stored in the current user session variables.

The class can generate HTML for a hidden input with using the generated key as input name and the value as input value.

Then the class can validate if the user is valid checking if the submitted form values match what is stored in the session variables.

Picture of Dave Smith
  Performance   Level  
Name: Dave Smith is available for providing paid consulting. Contact Dave Smith .
Classes: 51 packages by
Country: United States United States
Age: 59
All time rank: 608 in United States United States
Week rank: 21 Up2 in United States United States Up
Innovation award
Innovation award
Nominee: 32x

Winner: 7x

Recommendations

Captcha process for multiple php program files
Form page has to screen out non-humans to upload product images

Example

<?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>


Details

Class: random key value pair authentication Version: 0.1 10/14/2017 Released into the public domain Description: This class will authenticate human submissions through random key value pairs. Installation: Upload files to a web accessible location on your server (eg. public_html) Configuration: No configuration needed. Usage: This class uses a serialized object to store the last key value pair for authentication in the session. If you don't know what I am talking about, that is okay, it just means that you must instantiate the class after the session has been started. This allows human verification without any intreaction with the human. session_start(); include('rndkvp.class.php'); if( empty($_SESSION['humanCheck']) ){ $humanCheck = new rndkvp(10); }else{ $humanCheck = unserialize($_SESSION['humanCheck']); } 1. If the session has not been started elsewhere, start it before instantiating the class. 2. Include the class file. 3. If the class object has not already been saved to the session, instantiate as normal, otherwise load it from the session. rndkvp takes 1 argument... length = length of the generated key value pairs, defaults to 8 After all processing has been completed, the object must be saved to the session... $_SESSION['humanCheck'] = serialize($humanCheck); Refer to the test.php file for usage examples. Point your browser to this file to see how it works. Methods createFormInput() creates a hidden input form field containing the random key value pairs validate( $value ) validates the submitted value resetKVP( $length ) generates new random key value pairs Changelog 0.1 Initial beta release

  Files folder image Files (3)  
File Role Description
Accessible without login Plain text file manual.txt Doc. Usage Documentation
Plain text file rndkvp.class.php Class Main Class
Accessible without login Plain text file test.php Example Test usage

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:172
This week:0
All time:8,817
This week:660Up
User Comments (1)