Generating a random password with PHP

One of the simplest techniques for ensuring the quality of registrations to a website is to require confirmation via an e-mail address before providing access. Quickest way to do this is to not ask for a password but instead mail a random generated password to the provided address. If they then log in with the password you sent you know the e-mail address is valid.

But how do you generate a random password for every pending user? Well generating random strings is an art in itself. However one of the simplest and neatest techniques I’ve come across was described by Kevin Yank on Sitepoint:


$newpass = substr(md5(time()),0,6);

His command basically takes a substring of an encrypted version of the current time. A good chance that you’ll never get two users therefore with the same password.

You can change the length of the provided password by changing the “6″ at the end of the command.

Very neat. Very simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>