28, May 2012

Creating random passwords with PhP - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating random passwords with PhP
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Creating random passwords with PhP  (Read 1304 times)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« on: Nov 12, 2005, 12:18:09 pm »

Here is a snippet for creating random passwords with PhP :

Code:
<?php
if (!function_exists('str_split')) {
       function 
str_split($str$length 1)
       {
           
// Return false if parameter isn't string, length isn't numeric, or length is 0.
           
if (!is_string($str)|| !is_numeric($length) || $length == 0) {
               return 
FALSE;
           }
           
// Define array to be used to split.
           
$strArr = array();   
           
// Loop through all characters and stick them in the array.
           
for ($i 0$i strlen($str); $i += $length) {
               if (
strlen(substr($str$i)) < $length) {
                   
$strArr[] = substr($str$i);
               }
               else {
                   
$strArr[] = substr($str$i$length);
               }
           }            
           
// Return the array.
           
return $strArr;
       }
  }
  
function 
getRandPass($len=5){
    
$chrs "1234567890qwertyuiopasdfghjklzxcvbnm";
    
$ch str_split($chrs);
    for(
$i 0$i $len$i++)
    {
        
$pwd .= $ch[mt_rand(0count($ch))];
    }
    return 
$pwd;
}
?>


The $len = 5 indicates how many characters the password will contain.

Enjoy Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Raped By Google
*
Posts: 28
176 credits
Members referred : 0


« Reply #1 on: Nov 13, 2005, 06:28:25 pm »

I think you don't need to use the split function!
this should be enough with splitting!
$pwd .= $ch[mt_rand(0, count($ch))];
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #2 on: Nov 13, 2005, 06:38:52 pm »

Hmmm. You are right

I think this should work with brackets { not [

Thanks for correcting me Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Raped By Google
*
Gender: Female
Posts: 27
240 credits
Members referred : 0



« Reply #3 on: Nov 14, 2005, 11:52:22 am »

You can also do this :

Code:
<?php
function getRandPass($len=5){
    $pwd '';
    for($i 0$i $len$i++)
    {
        $pwd .= chr mt_rand35 125 ) );
    }
    return $pwd;
}
?>


I think that this is the faster way
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #4 on: Nov 14, 2005, 11:55:38 am »

You are right Archer. That's far faster than mine.

That's why it is good to share your code. You can allways make your code better Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Raped By Google
*
Posts: 28
176 credits
Members referred : 0


« Reply #5 on: Nov 14, 2005, 01:54:55 pm »

It is faster but not customizable to choose the character sets for the generated password!
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=731
Tags : php snippets Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Creating random passwords with PhP
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 08:42:00 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.