FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free 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 = "0123456789bcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
   
  // 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..
Related topics
Can someone help me ? Com domain setup?
Password denied
Resolved
Help!!! My DirectAdmin isn't working!!!
Direct Admin password invalid?
directadmin login
How to make 5frih$ (Limited Time only)
How to enter CPanel?
Problem connecting to MySQL database with PHP [RESOLVED]
Auto create tables
encrypt and decrypt PROBLEM plss HELP!
Wardriving
Random Password?
Python random password generator in one line
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

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