Current location: Hot Scripts Forums » Programming Languages » PHP » Create a random password


Create a random password

Reply
  #1 (permalink)  
Old 03-04-10, 11:45 AM
cedricmeier12345 cedricmeier12345 is offline
New Member
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Create a random password

Hello everyone,

I have the code below that will create a random password for me. I have the codelenght variable set to 10 which creates a random password with 10 characters maximum. The only problem is that sometimes it will create a password with less than 10 characters. How do i edit this so it will always create a random password of exactly 10 characters?

Thanks so much for any help you can provide. I have been at this for a while and don't know where to go from here!


<?
$codelenght = 10;
while($newcode_length < $codelenght) {
$x=1;
$y=3;
$part = rand($x,$y);
if($part==1){$a=48;$b=57;} // Numbers
$code_part=chr(rand($a,$b));
$newcode_length = $newcode_length + 1;
$newcode = $newcode.$code_part;
}
?>
Reply With Quote
  #2 (permalink)  
Old 03-04-10, 05:27 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Not my function, although I did add some functionality to it. But, it works, and will return a totally random password, and you can set the length.

PHP Code:

function createRandomPassword() {
$length 10;  //length of password;
    
$chars "abcdefghijkmnopqrstuvwxyz023456789";
    
srand((double)microtime()*1000000);
    
$i 0;
    
$pass '' ;
    while (
$i <= $length) {
        
$num rand() % 33;
        
$tmp substr($chars$num1);
        
$pass $pass $tmp;
        
$i++;
    }
    return 
$pass;

Reply With Quote
  #3 (permalink)  
Old 03-04-10, 08:01 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
PHP Code:

function random_password$length 10 )
{

    
$length = ( int ) $length;

    
$chars array_merge(
        
range'A''Z' ),
        
range'a''z' ),
        
range'0''9' )
    );

    
$pass '';

    while ( 
$length strlen$pass ) )
    {
        
$pass .= $charsarray_rand$chars ) ];
    }

    return 
$pass;


__________________
The toxic ZCE
Reply With Quote
  #4 (permalink)  
Old 03-04-10, 08:32 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

while ( $length strlen$pass ) )

    {
        
$pass .= $charsarray_rand$chars ) ];
    } 
could be:

PHP Code:

$pass=substr(shuffle($chars),0,$length-1); 

PHP: shuffle - Manual
Reply With Quote
  #5 (permalink)  
Old 03-05-10, 09:20 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by wirehopper View Post
PHP Code:

while ( $length strlen$pass ) )
    {
        
$pass .= $charsarray_rand$chars ) ];
    } 
could be:

PHP Code:

$pass=substr(shuffle($chars),0,$length-1); 

PHP: shuffle - Manual
Have you read that link you'd provided? shuffle() returns boolean, it's argument is passed as a reference.

And $chars is an array... substr() accepts a string as it's first argument.
__________________
The toxic ZCE
Reply With Quote
  #6 (permalink)  
Old 03-05-10, 10:08 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Ooops.

General idea was to use a library function instead of a loop.

I stand corrected.

PHP Code:

$pass=substr(shuffle($chars),0,$length-1); 

Should be

PHP Code:

$pass=substr(str_shuffle($chars),0,$length-1); 

PHP: str_shuffle - Manual
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Email Random Password ecohen ASP 4 07-30-04 10:32 AM
script to generate random username, password, etc. mangled Script Requests 1 01-19-04 07:15 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 08:11 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.