Zend - The PHP Company




Passwords

Add Code


password_lib  

Type: application
Added by: ljweb
Entered: 08/04/2003
Last modified: 04/12/2002
Rating: **** (7 votes)
Views: 5412
Module for generation of passwords based on different schemes from the more easy to the extreme passwords. Useful for autogeneration of passwords, for validationlinks ect.


<?php
/*
    File : password_lib.php
    Version : 1.0
    Date : November 21st, 2002
    Author : Lars B. Jensen, lars.jensen@ljweb.com

    Module Description
    Module to produce passwords.
*/
    # Seed the random generator - consider doing this in a config file once for the entire site
    
mt_srand((double)microtime()*1000000);


    function 
password_generate($nice 1$length=0$allowchars "") {
        
# Find random password length
        
if (!$length$length mt_rand(59);

        
# pronouncable password
        
if ($nice == 1) return password_generate_pronouncable($length);
        
# lowercase only, fix similar
        
else if ($nice == 2) return password_generate_advanced($length01001$allowchars);
        
# lowercase and numbers only, fix similar
        
else if ($nice == 3) return password_generate_advanced($length01101$allowchars);
        
# both lower and uppercase chars and numbers , fix similar
        
else if ($nice == 4) return password_generate_advanced($length11101$allowchars);
        
# all types of letters, including special chars, fix similar
        
else if ($nice == 5) return password_generate_advanced($length11111$allowchars);
        
# oh my :) the real deal - get it all and dont fix similars
        
else if ($nice == 6) return password_generate_advanced($length11110$allowchars);

        
# $nice contained illegal value, go for the easy 3
        
else return password_generate_advanced($length11101);
    }


    function 
password_generate_advanced($length 8$allow_uppercase 1$allow_lowercase 1$allow_numbers 1$allow_special 1$fix_similar 0$valid_charset "") {
        
# Create a list of usable chars based upon the parameters
        
if (!$valid_charset) {
            if (
$allow_uppercase$valid_charset .= 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
            if (
$allow_lowercase$valid_charset .= 'abcdefghijklmnopqrstuvxyz';
            if (
$allow_numbers$valid_charset .= '0123456789';
            if (
$allow_special$valid_charset .= '!#$%&()*+-./;<=>@_';
        }
        
# Find the charset length
        
$charset_length strlen($valid_charset);

        
# If no chars is allowed, return false
        
if ($charset_length == 0) return false;

        
# Initialize the password and loop till we have all
        
$password "";
        while(
strlen($password) < $length) {
            
# Pull out a random char
            
$char $valid_charset[mt_rand(0, ($charset_length-1))];
            
            
# If similar is true, check if string contains mistakeable chars, add if accepted
            
if (($fix_similar && !strpos('O01lI5S'$char)) || !$fix_similar$password .= $char;
        }


        return 
$password;
    }


    function 
password_generate_pronouncable($length 8) {
        
# Initialize valid char lists
        
$valid_consonant 'bcdfghjkmnprstv';
        
$valid_vowel 'aeiouy';
        
$valid_numbers '0123456789';

        
# Find the charset length
        
$consonant_length strlen($valid_consonant);
        
$vowel_length strlen($valid_vowel);
        
$numbers_length strlen($valid_numbers);

        
# Initialize the password and loop till we have all
        
$password "";
        while(
strlen($password) < $length) {
            
# Pull out a random set of pronouncable chars
            
if (mt_rand(02) != 1$password .= $valid_consonant[mt_rand(0, ($consonant_length-1))].$valid_vowel[mt_rand(0, ($vowel_length-1))].$valid_consonant[mt_rand(0, ($consonant_length-1))];
            else 
$password .= $valid_numbers[mt_rand(0, ($numbers_length-1))];
        }

        return 
substr($password0$length);
    }
?>


Usage Example




Rate This Script





Search



This Category All Categories