Auto Password / Unique id Generation
Overview: You might have come across sites, that generate a unique username or a randomly generated password for you, this script show you how to do the same with PHP.
It is a common practice for websites to issue you a randomly generated password and I will show how this can be done via PHP, this concept can also be used to generate random passwords, you can generated a new password via this method for a user that might have lost his password and email this new password to him.
Okay enough of talk now, I will show you how we will proceed to accomplish it, I will be using PHP built in functions uniqid and crypt to accomplish the task.
What basically ëuniqidí does is it generated a UNIQUE id based on the current time in microseconds and we use the ëcryptí function to do a one way encryption of the same.
The code given below will generate a random 10 digit id that can be used as a unique id or password.
//set the random id length
$random_id_length = 10;
//generate a random id encrypt it and store it in $rnd_id
$rnd_id = crypt(uniqid(rand(),1));
//to remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));
//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));
//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);
echo "Random Id: $rnd_id";
Note If you require a password to be of specific length just substitute the $random_id_length variable with the length required, Generally a 8 - 12 digit password / id is considered to be highly secure and very difficult to predict.
(c) 2002 - 2018 www.PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.