<?php // The letter l (lowercase L) and the number 1 // have been removed, as they can be mistaken // for each other. function RandomPass() { // Comment the first line to exclude special characters $chars = "!@#$%^&*"; $chars .= "abcdefghijkmnopqrstuvwxyz023456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; srand((double)microtime()*1000000); $i = 1; $pass = '' ; // Number of digits we'd like to have our password be $n = 8; while ($i < $n) { $num = rand() % strlen($chars); $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } // Usage $password = RandomPass(); echo "Random password is: $password"; ?>
I’m just learning PHP for use with osTicket and reading some of your code as examples. I am finding your postings very useful.
In this one, shouldn’t the phrase “rand() % 33” be “rand() % strlen($chars)” so that the random character is selected from the entire list of available characters? With 33, the passwords are always lower case in your example.
Oops! Yes, thank you. It was originally 33 characters long but I added more options including the capitals and forgot to update that as well.
Thanks!