PHP Function Lookup:
PHP Quick Start
PHP Installation
PHP Articles
PHP Scripts

Top Rated Articles 
  • Simple Ad Rotator
  • PHP and Cookies
  • Getting Screen resolution using JavaScripts & PHP
  • Using Functions in PHP
  • PHP Sessions

  • Site Related
    Submit Articles/Code
    Contact Us

       Home                   Article Added on: February 27, 2003
    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.

    random_id.phpView Sample Output

    //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.

    Rate this article:  Current Rating: 4.67
      Poor    Excellent     
              1     2    3    4    5


    Home | Privacy Policy | Contact Us | Terms of Service
    (c) 2002 - 2019 Unauthorized reproduction/replication of any part of this site is prohibited.