Is there any function to create a string with random characters and numbers? This string will be used to create passwords..
Is there any function to create a string with random characters and numbers? This string will be used to create passwords..
I don't think PHP support a function that can be used to generate a password or a security code. You have to develop it using a custom algorithm. You can also find lots of random password generators in PHP through google.
PHP Code:
function randomAlphanumericPassword($length = 5){
$characters = array_merge(range('a', 'z'), range(0, 9));
shuffle($characters);
$password = array_slice($characters, 0, abs($length));
return implode($password);
}
//EXAMPLE
echo randomAlphanumericPassword(6);
borris83 (03-31-2009)
Hi Techietim!
The function works great.. Thanks
That is pretty slick Tim. I've never seen one done that way.
Only thing I would do differently is get rid of 0 (Zero), 1 (one) and l (lowercase L).
Nice job.
PHP Code:
$characters = array_merge(range('a', 'k'), range('m', 'z'), range(2, 9));
Last edited by JasonDFR; 03-31-2009 at 02:25 PM. Reason: Forgot to get rid of l
I can understand the lowercase L being removed, but why take out 1?
Same reason. Depending on the font, 1 and l can look similar. Get rid of O and I as well if allowing uppercase letters. When something as simple as this can avoid potential problems for some users, why not?
Fair enough but if it's going to be part of a hyperlink that's randomly generated then there's no point in removing specific letters since the user will click it and not need to worry about there being 1s that look like ls.
But of course, if this is going to be for a different purpose then what you say is true and removing those characters will help to avoid confusion.
I seem to have lost the ability to read today =/
Bookmarks