View Single Post
Old 11-03-2007, 12:31 PM   · #2
Daniel
Danltn.com
 
Daniel's Avatar
 
Name: Daniel Neville
Location: Danltn.com / Nottingham, UK
Trader Rating: (65)
Join Date: May 2007
Posts: 1,185
NP$: 0.56 (Donate)
Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute
Ethan Allen Fund Ethan Allen Fund
PHP Code:
<?php

/**
* Generate a random password.
*
* @param  integer  $numchars      How long do we need the password to be?
* @param  boolean  $specialchars  Include the special characters?
* @param  boolean  $extrashuffle  Include an extra randomization on the password string?
* @param  boolean  $mixedcase     Mixed case or solely lowercase?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false, $mixedcase = true)
{
    
$numchars = intval($numchars);
    
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;
    
    
$chars = array_merge(range('a', 'z'), range(0, 9));

    if (
$mixedcase) /* Extra case */
    
{
        
$chars = array_merge($chars, range('A','Z'));
    }
    
    if (
$specialchars)
    {
        
$chars = array_merge($chars, array('!', '$', '_', '-', '#', '@'));
    }
    
shuffle($chars);

    
$pass = '';

    for (
$i = 0; $i < $numchars; $i++) /* Changed to < only sign, otherwise it would be 1 character longer than needed */
    
{
        
$pass .= $chars[$i];
        
shuffle($chars); /* Get repeated characters, would add as an option, but I'm lazy */
    
}

    if (
$extrashuffle)
    {
        return
str_shuffle($pass);
    }
    return
$pass;
}

// Example, returns: 3ck#4si2
echo random_pass(8, true, true, true);

?>


Added an extra shuffle after each character, so you can get repeated characters too. (I prefer it that way).
Added MiXeD case option
Fixed the length ($i = 0; $i <=$numchars) part.


Please register or log-in into NamePros to hide ads
Daniel is offline   Reply With Quote
Site Sponsors
Exdon Find out how! http://www.mobisitetrader.com/
Advertise your business at NamePros
All times are GMT -7. The time now is 03:18 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.