ss_blog_claim=88d0a386a6277415f42c9ee5561ded98

Archive for June 20th, 2007

Generating Random Passwords in PHP

So here is my first coding article for this site and I thought I’d start with something simple: generating random passwords using PHP.

Generation of random passwords is very practical in many situations, for example generating forum activation codes, but it doesn’t stop there. There are virtually thousands of uses for a script like this.

The script becomes more versatile and scalable if it is written in the form of a function, so that is exactly what I will do. This function will hopefully work anywhere.

The basic idea behind this random password is that it will give a different password every time it is called. This can only be done by using the time as what the password is generated off to make each code truly unique. So we will start of by defining a PHP function which will generate this password for us. There will be one input from the user: the length of the password. When the function is called, it will return a random password of alphanumeric characters ranging from 0-32 depending on the length given.

Here is the complete function:

<?php
function randomPassword($length){
 
  $pass = md5(time());
  $pass = substr($pass, 0, $length);
  return $pass;
}
?>

Download this code: genrandpw.phps

The above function is pretty straightforward. The first line defines a function called randomPassword which takes a parameter ‘length’. The next line uses the built-in PHP function MD5(). MD5 function returns the hash of the input given. Here we give MD5 the time as an input. The time() function returns the timestamp. Remember that the timestamp will always be different because the date is always changing (here date includes time and seconds). So basically when we give the MD5 function the latest timestamp, it generates the hash for that specific instant, which is also always different because of the ever changing time.

Next we substr the password returned. Substr is used for returning some specific length of a given string. For example, the substr used here will return the initial string of the password of the length that the user will give.

The last line of code returns the password randomly generated.

The code can be used in such a way as shown in the examples below:

<?php
echo randomPassword(15);
 
// Or you could use the below and call the string via the $password variable.
 
$password = randomPassword(15);
echo $password;
?>

Download this code: genrandpw-examples.phps

The above code will return something like 1n48a6fb3m9f6c8

I hope you enjoyed the first code article on this blog, which should be the sign of more articles to come.

Tips, questions or just plain baffled by my code? Leave a comment.

If you liked this post, buy me a coffee!




June 2007
M T W T F S S
« May   Jul »
 123
45678910
11121314151617
18192021222324
252627282930  

RSS  


Posts (RSS) Posts     Comments (RSS) Comments
 Add to Technorati Favorites


advertisements  






top commentators