Zend - The PHP Company




Passwords

Add Code


Daily Random Password  

Type: code fragment
Added by: b3nz0
Entered: 30/10/2006
Last modified: 02/11/2005
Rating: - (fewer than 3 votes)
Views: 5844
A simple snippet to generate a random password that changes daily. * Note: The password will always be the same when generated during the same day with the same settings. Ideal for daily access codes etc.


<?php

/**
 * @desc Daily Random Password Generator
 * @author B3NZ0 Email: http://www.zend.com/mail_to.php?id=B3NZ0
 * 
 * @param int $pass_len The length of the password
 * @param bool $pass_num Include numeric chars in the password?
 * @param bool $pass_alpha Include alpha chars in the password?
 * @param bool $pass_mc Include mixed case chars in the password?
 * @param string $pass_exclude Chars to exclude from the password
 * @return string The password
 */
function make_daily_password($pass_len 8$pass_num true$pass_alpha true$pass_mc true$pass_exclude '')
{
    
// Create the salt used to generate the password
    
$salt '';
    if (
$pass_alpha) { // a-z
        
$salt .= 'abcdefghijklmnopqrstuvwxyz';
        if (
$pass_mc) { // A-Z
            
$salt .= strtoupper($salt);
        }
    }

    if (
$pass_num) { // 0-9
        
$salt .= '0123456789';
    }

    
// Remove any excluded chars from salt
    
if ($pass_exclude) {
        
$exclude array_unique(preg_split('//'$pass_exclude));
        
$salt str_replace($exclude''$salt);
    }
    
$salt_len strlen($salt);

    
// Seed the random number generator with today's seed & password's unique settings for extra randomness
    
mt_srand ((int) date('y')*date('z')*($salt_len+$pass_len));

    
// Generate today's random password
    
$pass '';
    for (
$i=0$i<$pass_len$i++) {
        
$pass .= substr($saltmt_rand() % $salt_len1);
    }

    return 
$pass;
}

?>


Usage Example


<?
// Configure Password

// Passwords length
$pass_len 8;
// Use numbers?
$pass_num true;
// Use letters? (e.g. abcde)
$pass_alpha true
// Use mixed case? (e.g. AbcaBc)
$pass_mc true;
// Exclude any chars from password? (e.g. oO0iI1l)
$pass_exclude 'oO0iI1l';

// Generate password
echo $pass make_daily_password($pass_len$pass_num$pass_alpha$pass_mc$pass_exclude);
?>

Example passwords:

QjWyUhKB
wx6YfbFj
rNWUtxHF

40361508
62855903
43738579


Rate This Script





Search



This Category All Categories