BigToach.com

PHP Snippets

random_pass

Information

(PHP Version >= 3.0.6)
Functions used in this snippet: strlen, mt_rand.

Description

str random_pass( [int length, [str chars]] )

Creates a random password length characters long with the specific characters of chars.

Snippet

<?php
function random_pass($length=7,$chars=NULL){
    if(
$chars === NULL){
        
$chars  '0123456789';
        
$chars .= 'abcdefghijklmnopqrstuvwxyz';
        
$chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    }
    
$charlen strlen($chars) - 1;
    
$ret '';
    for(
$i=0$i<$length$i++){
        
$ret .= $chars{mt_rand(0,$charlen)};
    }
    return 
$ret;
}
?>

Example

random_pass Can be used in the following way:

<?php
echo random_pass(); // 7 character random pass

echo random_pass(10); // 10 character random pass

echo random_pass(6,'abcdefghijklmnopqrstuvwxyz');
// 6 character random pass with only lowercase a-z characters
?>

Added on Aug 12th at 5 am by Scott - 0 Comments


0 Responses to random_pass

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: