Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-06-2011, 03:05 PM   PM User | #1
makintyre
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
makintyre is an unknown quantity at this point
How To Generate Random Passwords With PHP

PHP Code:
$length =10;
$characters_to_use ="abcdef1234567890";

for(
$i 0$i $length$i++)
{
$do mt_rand(0,strlen($characters_to_use)-1);
$password $password $characters_to_use{$do};
}

echo 
$password
makintyre is offline   Reply With Quote
Old 01-27-2012, 04:52 AM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
IMHO the characters 1 and 0 should be left out as they can be confused with l and O...

This is my passwdgenmachinefunctionthingymajig...

PHP Code:
function create_random_password($len=7){
    
$chars "abcdefghijkmnpqrstuvwxyz23456789";
    
srand((double)microtime()*1000000);
    
$i 0;
    
$pass '' ;
    while (
$i <= $len) {
        
$num rand() % 33;
        
$tmp substr($chars$num1);
        
$pass $pass $tmp;
        
$i++;
    }
    return 
$pass;


Last edited by jmj001; 01-27-2012 at 04:55 AM..
jmj001 is offline   Reply With Quote
Old 01-27-2012, 02:18 PM   PM User | #3
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Since years and years ago there is no longer a need to seed the random number generator with srand().

Also, mt_rand() is much faster than rand().
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 01-27-2012, 04:10 PM   PM User | #4
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
I must admit, I've been using that function for close to 10 years... so it's probably well outdated.

I still however recommend taking out the 1 & 0 so people don't confuse them .. just saying
jmj001 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:24 AM.


Advertisement
Log in to turn off these ads.