Like this code?
Show the author your appreciation.
This code exmaple also shows how to get values from an array using ranges.
Usage Example
<?php
require_once "password.class.php" ;
$password = new PasswordCreator ();
echo $password -> getPassword ( 64 , PC_MINUS ). "\n" ;
echo $password -> getPassword ( 64 , PC_MINUS | PC_CAPITAL ). "\n" ;
echo $password -> getPassword ( 64 , PC_MINUS | PC_CAPITAL | PC_NUMBERS ). "\n" ;
echo $password -> getPassword ( 64 , PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES ). "\n" ;
echo $password -> getPassword ( 64 , PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES | PC_EXTENDED ). "\n" ;
echo "\n" ;
?>
password.class.php
<?php
/*
* Simple password generator
*
* Copyright (C) 2007 Victor Roman Archidona
* http://blog.daijo.org
*/
define ( 'PC_MINUS' , 0x01 );
define ( 'PC_CAPITAL' , 0x02 );
define ( 'PC_NUMBERS' , 0x04 );
define ( 'PC_SPACES' , 0x08 );
define ( 'PC_EXTENDED' , 0x10 );
class PasswordCreator
{
/**
* Contains the ASCII table representation
*
* @access private
* @var array
*/
protected $_ascii_table = null ;
public function __construct ()
{
$this -> populateAsciiTable ();
}
/**
* Gets a random password with the specified length and flags
*
* @param int $length
* @param int $flags
* @return string
*/
public function getPassword ( $length = 16 , $flags = PC_MINUS )
{
/* If there is no length we throw an exception: */
if ( $length < 1 )
throw new Exception ( "Password lenght must be at least 1 character lenght" );
/* Get the character matrix and its lenght: */
$characters = $this -> getPasswordCharacters ( $flags );
$characters_length = count ( $characters );
/* Build the password: */
for ( $i = 0 ; $i < $length ; $i ++) {
$random = rand ( 0 , $characters_length - 1 );
$output .= $characters [ $random ];
}
/* Return it: */
return $output ;
}
/**
* Gets the final character matrix with the specified flags.
*
* @param int $flags
* @return array
*/
protected function getPasswordCharacters ( $flags )
{
$chars_to_use = array();
if ( $flags & PC_MINUS ) {
$chars_minus = $this -> getAsciiRange ( 'a' , 'z' );
$chars_to_use = array_merge ( $chars_to_use , $chars_minus );
}
if ( $flags & PC_CAPITAL ) {
$chars_mayus = $this -> getAsciiRange ( 'A' , 'Z' );
$chars_to_use = array_merge ( $chars_to_use , $chars_mayus );
}
if ( $flags & PC_NUMBERS ) {
$chars_numbers = $this -> getAsciiRange ( 48 , 57 );
$chars_to_use = array_merge ( $chars_to_use , $chars_numbers );
}
if ( $flags & PC_SPACES ) {
$chars_spaces = $this -> getSpaces ();
$chars_to_use = array_merge ( $chars_to_use , $chars_spaces );
}
if ( $flags & PC_EXTENDED ) {
$chars_ext = $this -> getAsciirange ( 128 , 255 );
$chars_to_use = array_merge ( $chars_to_use , $chars_ext );
}
return $chars_to_use ;
}
/**
* Gets the ASCII characters between the specified range
*
* @param int $start First character
* @param int $end Last character
* @return array
*/
protected function getAsciiRange ( $start , $end )
{
$start = is_numeric ( $start ) ? $start : ord ( $start );
$end = is_numeric ( $end ) ? $end : ord ( $end );
for ( $i = $start ; $i <= $end ; $i ++)
$output [] = $this -> _ascii_table [ $i ];
return $output ;
}
/**
* Gets the space characters (tab, newline and space)
*
* @return array
*/
protected function getSpaces ()
{
$spaces = array( "\t" , "\n" , " " );
foreach ( $spaces as $space )
$output [] = ord ( $space );
return $output ;
}
/**
* Populates the internal ASCII table
*/
private function populateAsciiTable ()
{
for ( $i = 0 ; $i <= 255 ; $i ++)
$this -> _ascii_table [] = chr ( $i );
}
}
?>
A damaged image generator (class) for validating text.
CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart Categories : PHP , PHP Classes , Security , GD image library , Security Generating and Matching Secure and Strong Password Hash Categories : PHP , PHP Classes , Cryptography , Security Dollar Serial Number Validator Categories : PHP , Security , Algorithms Use of bitmasks to represent permissions Categories : PHP , Authentication , Bitwise Operators , Security , PHP Classes Object() = Custom __autoload + Singleton. "automagically" instantiates a class and always retuns the same instance of the same class. It's pretty useful when you want to have persistence in objects. Categories : PHP , PHP Classes , Algorithms SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge
response login algorithms over an insecure connection. Categories : Algorithms , PHP , Security Scan Apache access log files and report possible worms attack Categories : PHP , PHP Classes , Security , Apache , Log Files Scramble Eggs - php class to scramble/encode Categories : PHP , PHP Classes , Security , Encryption A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms , Variables , Arrays , PHP , PHP Classes PHP Function to Encrypt/Decrypt a string without a known key. The string itself has his own different key for every character. Categories : PHP , Algorithms , Security , Authentication , Encryption Antispoof - a class to help prevent people hi-jacking and misusing parts of a website Categories : PHP , PHP Classes , Security Timer - a class that uses microtime() to provide easy calculation of elapsed times Categories : Algorithms , PHP , PHP Classes IPhider Obscure Any URL Anonymity connection lores obfuscation corporate survival. Categories : PHP , Algorithms , Security , URLs Forms protected from XSS attacks (FOPAXSS) Categories : PHP , PHP Classes , Form Processing , Security Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers. Categories : PHP , PHP Classes , Credit Cards , Ecommerce , Algorithms