Generate A Random Password And OTPs

Passwords are important for security reasons. Now we will create simple passwords that are difficult to hack. You can do this by using special characters in passwords.

Machine generated random passwords are complex and difficult to hack. These types of passwords are strong and more secure. Now we will see how to create strong passwords using PHP code.

PHP

PHP is an acronym for "PHP: Hypertext Pre-processor." It is a widely-used, open source scripting language. PHP scripts are executed on the server. It is free to download and use. PHP is an amazing and popular language!

Before we take the next step to create strong passwords using PHP scripts, we should decide on what characters to include in the passwords.

To create a string named $character and assign a value to it,

  1. $characters = "1234567890"  

 We use the str_shuffle() to shuffle randomly in this string value.

  1. $val = str_shuffle($characters);  

Now we can get a value that's randomly shuffled for all the characters of a string passed. Then we set a character limit of the password using substr()

Every time the program is executed, it displays a different output since shuffling of characters is different every time. The original string or the number can be the return value on some occasions

substr()

The substr() function returns a part of a string.. this is the syntex of substr(), these are parameters passed.

  1. substr(string,start,length); 

 

  • string Required
    Specifies the string to return a part of

  • start Required
    Specifies where to start in the string.

  • length Optional
    Specifies the length of the returned string. Default is to the end of the string.

 

Ok, now I want to create a password using letters, numbers and some special characters; letters as small letter and capital letter as combined. For this I assign this in a string value.

  1. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"

 

I use the str_shuffle function and shuffle these letters..

  1. str_shuffle( $chars )  

 

If required to set a limit of the password, use a substr function and set the limit of the word.

  1. $password = substr( str_shuffle( $chars ), 0, 9);  

 

Example

  1. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";  
  2. $password = substr( str_shuffle( $chars ), 0, 9);  

 

Create a OTPs

  1. $numbers = "1234567890"  
  2. $otp = substr( str_shuffle( $ numbers), 0, 4);  

 

This is a simple way to create a random password using php code.

Thanks!