Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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 05:55 AM.

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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().

  • #4
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •