dcsimg

What is your dream?

What do you do?

That's right: You

This is about you, your goals, your passions.

Figure that out and we'll help you take it to the world, shouting it from the rooftops. We create the websites that enable you to achieve this dream.

Random PHP Password Generator

Monday, February 20, 2012 11:05 PM

A project I was working on the other day needed an automatically generated password. I had an old script that spat out a really basic alphanumeric 6-character password, but I wanted something a bit stronger and more versatile. I thought I’d share. This code will give you a default of 8 characters, alphanumeric plus special characters, with flexibility for whatever length you want. The implementation is nothing fancy.

Code:

Random Password: <br />
<?php
$len = isset($_GET['len']) ? $_GET['len'] : 8;
$pw = '';
for($i=0; $i<$len; $i++) {
	$pw .= chr(rand(33, 126));
}
echo $pw;
?>

Output example:

W?7;q,%R

The characters it uses are pulled from the ASCII table (values 33 through 126, inclusive).

Just save that on your server and pull it up. Add a ?len=N query string in there were N is a number for the desired non-default length.

Caveat: All of that said, don’t forget that there are better choices than randomized passwords.

Thanks TravInSF for idiot-checking me

Comments

Commenting is not available in this channel entry.

Want to work together or maybe just say hello?