Computersight > Programming > PHP

Generate Random Password with PHP

A PHP function which lets yo generate random passwords.

This function generated a random password of a desired length. The default length is 10 but you can easily change the length by passing the desired length to the function.

function randomPassword ($passwordLength = 10) { // First we start with an empth password $randPassword = ''; // Then we say which characters we want to have in the random password $allowedCharacters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // We add the desired number of characters to the password for ($i=0; $i < $passwordLength; $i++) { // We randomly choose a new character $character = substr($allowedCharacters, mt_rand(0, strlen($allowedCharacters)-1), 1); // And add it to the randmom password $randPassword .= $character; } // Simply return the generated password and DONE return $randPassword; } ?>

And this is how it is being used.

Save the function in a file and call it password.php

Then in the same folder create a file and call it passwordtest.php

In the file password test write the following code

include('password.php'); echo "A random password with default length: "; echo randomPassword(); echo "
A random password with a length of 6 characters: "; echo randomPassword(6); ?>

Hope you can use this function.

0
Liked It
I Like It!
Related Articles
Windows XP Automatic Password Generation and Assignment  |  Cpanel Automation
More Articles by stephan
Adobe Photoshop Express  |  Cpanel Automation
Latest Articles in PHP
What is PHP? Learn the Basics  |  PHP Tutorial: Printing on the Screen
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.