PHP Classes

PHP Secure Token Generator: Create and validate tokens stored in MySQL table

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: 334 All time: 7,121 This week: 71Up
Version License PHP version Categories
itoken 1.0.0BSD License5PHP 5, Databases, Validation, Security
Description 

Author

This class can generate and validate tokens stored in a database.

It can generate a unique token and store it in a database table using PDO.

The token values can be valid for a given expiry time that by default it is 60 seconds.

The class can also validate the token value by checking if it exists and is still valid by looking at the token expiry time.

The token may also be refreshed to extend its lifetime, or destroyed so it is no longer valid.

Picture of Aleksandar Zivanovic
  Performance   Level  
Name: Aleksandar Zivanovic <contact>
Classes: 16 packages by
Country: Serbia Serbia
Innovation award
Innovation award
Nominee: 4x

Example

<?php
/**
 * User: Aleksandar Zivanovic <coapsyfactor@gmail.com>
 */

/** Load required class */
require_once __DIR__ . '/iToken.php';

$dbHost = 'db_host';

$dbUser = 'db_user';

$dbPass = 'db_pass';

$dbName = 'db_name';

$dbPort = 3306;

// Connect to database ($dbPort is not required, if not set default value is used, default value of port is 3306)
iToken::establishDatabaseConnection($dbHost, $dbUser, $dbPass, $dbName, $dbPort);

// Create itokens table if it doesn't exists
iToken::executeInitialSQL();

// Get iToken instance
$iToken = new iToken();

// Generate normal token
$token = $iToken->generate();

echo
"Token created: {$token}\n";

echo
"Token {$token} is valid {$iToken->isValid($token)}\n";

// Check is token is valid
if ($iToken->isValid($token)) {

   
// Remove token
   
$iToken->destroyToken($token);

    echo
"Token {$token} removed.\n";

}

// Generate entity token
$entityToken = $iToken->generate('EntityID');

echo
"Entity Token created: {$entityToken}\n";

echo
"Entity Token {$entityToken} is valid {$iToken->isValid($entityToken, 'EntityID')}\n";

// Check is entity token valid
if ($iToken->isValid($entityToken, 'EntityID')) {

   
// Remove all tokens for given entity
   
$iToken->destroyEntityTokens('EntityID');

    echo
"Entity Tokens removed.\n";

}


  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file example.php Example Example script
Plain text file iToken.php Class Main class

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:334
This week:0
All time:7,121
This week:71Up