FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Make an random password..

 


ranzon
hi..
i'm looking for the function that creates an random password, on 6 letters, wich exists of numbers from: 0 to 9 and a to z + A to Z.

please help me..
kv
Quick search on google gave this URL

http://www.laughing-buddha.net/jon/php/password/

Good one. Just search google, you will get lot of them.

http://www.google.com/search?q=php%20generate%20random%20password
ranzon
kv wrote:
Quick search on google gave this URL

http://www.laughing-buddha.net/jon/php/password/

Good one. Just search google, you will get lot of them.

http://www.google.com/search?q=php%20generate%20random%20password


i found the first script, but it dont return anything:
Code:
<?php
function generatePassword ($length = 8)
{

  // start with a blank password
  $password = "";

  // define possible characters
  $possible = "0123456789bcdfghjkmnpqrstvwxyzæøåABCDEFGHIJKLMNOPQRSTUVWXYZÆØÅ";
   
  // set up a counter
  $i = 0;
   
  // add random characters to $password until $length is reached
  while ($i < $length) {

    // pick a random character from the possible ones
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
       
    // we don't want this character if it's already in the password
    if (!strstr($password, $char)) {
      $password .= $char;
      $i++;
    }

  }

  // done!
  return $password ;

}
?>
hexkid
ranzon wrote:
i found the first script, but it dont return anything:
Code:
<?php
function generatePassword ($length = 8)
{
// ...
}
?>


This code defines a function but does not call it.
You have to call it, and output the result.

Code:
<?php
function generatePassword ($length = 8)
{
// ...
}

for ($i=0; $i<10; ++$i) {
  echo generatePassword(12), '<br>';
}
?>
AftershockVibe
When you say "return" do you mean it doesn't print anything or does itn't return a value from the function?

I'm not sure about variable assignment in parameters either...

Try the following:
Code:
<?php
function generatePassword($length)
{

  // start with a blank password
  $password = "";

  // define possible characters
  $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   
  // set up a counter
  $i = 0;
   
  // add random characters to $password until $length is reached
  while ($i < $length) {

    // pick a random character from the possible ones
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
       
    // we don't want this character if it's already in the password
    if (!strstr($password, $char)) {
      $password .= $char;
      $i++;
    }

  }

  // done!
  return $password ;

}

$RandomPass = generatePassword(8);

echo $RandomPass;
?>


You probably don't want to echo it but this is a good test. I also removed some of the untypable characters and lower case vowels and 'l' (As they were missing for some reason).
ranzon
AftershockVibe wrote:
When you say "return" do you mean it doesn't print anything or does itn't return a value from the function?

I'm not sure about variable assignment in parameters either...

Try the following:
Code:
<?php
function generatePassword($length)
{

  // start with a blank password
  $password = "";

  // define possible characters
  $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   
  // set up a counter
  $i = 0;
   
  // add random characters to $password until $length is reached
  while ($i < $length) {

    // pick a random character from the possible ones
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
       
    // we don't want this character if it's already in the password
    if (!strstr($password, $char)) {
      $password .= $char;
      $i++;
    }

  }

  // done!
  return $password ;

}

$RandomPass = generatePassword(8);

echo $RandomPass;
?>


You probably don't want to echo it but this is a good test. I also removed some of the untypable characters and lower case vowels and 'l' (As they were missing for some reason).


it worked Smile
thanks for all help..
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.