Simple way to generate random password using php

In this tutorial, I will show you how to create or generate random password using PHP. It is better to use random password rather than using your date of birth or name or email id as password which can be easily hacked.

By using the following PHP function you can generate multiple random passwords. The password may contain uppercase, lowercase characters, numbers, and symbols. The characters in each set are user-friendly because there are no ambiguous characters such as 0,O,i,1,l,L  but, you can have it if you want.

Simple way to generate random password using php

PHP – Generate Random Password Using PHP (index.php)

<?php
    function passwordGenerator($password_length, $number_of_password, $password_strength = '') {

      $characters['uc'] = 'ABCDEFGHJKMNPQRSTUVWXYZ';  //Uppercase Characters
      $characters['lc'] = 'abcdefghjkmnpqrstuvwxyz';  //Lowercase Characters
      $characters['n']  = '23456789';	              //Numbers
      $characters['s']  = '@!#$%^&()_+={}][?';        //Symbols

      $password_array = [];
      $characters_to_be_used = '';

      if($password_strength != '') {
        $password_strength = explode(",", $password_strength);
        foreach($password_strength as $key=>$value) {
          $characters_to_be_used .= $characters[$value];
        }
      } else {
        //if $password_strength is leave blank, it will combine all the character types
        $characters_to_be_used = implode("",$characters);
      }

      $length = strlen($characters_to_be_used) - 1;


      for($i=0; $i<$number_of_password; $i++) {
        $new_password = '';
        for($j=0; $j<$password_length; $j++) {
          $index = rand(0, $length);
          $new_password .= $characters_to_be_used[$index];
        }

        $password_array[] = $new_password;
      }

      return $password_array;
   }
?>

Here in the above PHP script for generate random password we created a function named passwordGenerator($password_length, $number_of_password, $password_strength) {} where $password_length is the length of the password it should be. $number_of_password is how many passwords you want to generate. And $password_strength is the combination of character types that you want to use in your random password. For example, ‘uc,lc,n,s’ – means you want to generate random password using uppercase, lowercase characters, numbers, and symbols. You can leave this variable blank, by default, it will generate random password by combining all the character types. After generates the password(s) it returns an array of passwords.

Call The Function passwordGenerator(…) (index.php)

Here are some examples of how to generate random password using PHP

//Generate one random password of length 6 characters
passwordGenerator(6, 1);

//Generate two random password of length 6 characters
passwordGenerator(6, 2, 'uc,lc,n,s');

//Generate three random password of length 8 characters
passwordGenerator(8, 3, 'lc,n,s');

//Generate four random password of length 10 characters
passwordGenerator(10, 4, 'n,s');

Complete Source Code – Generate Random Password Using PHP

Download the complete source code for generating the random password using PHP from the below download link. Also, you can try the demo from the below demo link. Please like and share this article with others.

1 thought on “Simple way to generate random password using php

Leave a Reply

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