How to generate random password using PHP?
Strong password should be,
- At least eight characters long.
- Very good combination of letters, numbers, and special characters.
- Don't use dictionary words.
- Don't use your phone number,reverse order of your name,nick name,NIC number,Passport number, or mobile number.
- A good combination of simple and capital letters.
- Don't use characters or numbers only.
- Memorized easily without save it to a other media.
- Change your password periodically.
Don't use dictionary words or names as your password. Because it cause to a dictionary attack. Also it is important to keep separate password for your other e-mail or computer accounts.
When you make a password, you have to keep above guidelines in your mind. I f you use a strong password, it is difficult to break the password using automated computer programs. (robot programs) Working with web applications you may be always need to generate random passwords in PHP. Suppose you have a user registration form. After a user registration you have to assign a random password to his account and e-mail login details. So definitely you need to generate random passwords for this case. Following examples describe you how to generate multiple passwords using PHP.
View Live Demo - PHP Random Password Generator
1. How to create random passwords in PHP using numbers?
This example describe you to generate random password with numbers in PHP. Generated password contains integers 0 to 9. But be careful using numerical passwords because it can be break using automated computer programs.
<?php
function generatePasswordsUsingNumbers($length){
$numbers='0123456789';
$password_characters = str_split($numbers);
$password='';
for($i=0;$i<$length;$i++){
$password.=$password_characters[rand(0,count($password_characters)-1)];
}
return $password;
}
echo generatePasswordsUsingNumbers(8);
?>
2. How to create random passwords in PHP using alphabetic characters?
Sometimes you may need to create passwords using alphabetic characters. Bellow function explains make random password in php. Populated password contains a random set of simple and capital letters. You can change the length of the password.
<?php
functiongeneratePasswordsUsingCharacters($passwordLength){
$letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$password_characters = str_split($letters);
$password='';
for($i=0;$i<$passwordLength;$i++){
$password.=$password_characters[rand(0,count($password_characters)-1)];
}
return $password;
}
echo generatePasswordsUsingCharacters(8);
?>
3. How to create random passwords in PHP using alphabetic characters and numeric characters?
When you need php random passwords with a combination of letters and numbers, use bellow function to create passwords. You can make strong passwords using this method. You can pass the parameter "password length" to change the length of the created password. Use higher value to make it strong.
<?php
functiongeneratePasswordsUsingCharactersAndNumbers($passwordLength){
$characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$password_characters = str_split($characters);
$password='';
for($i=0;$i<$passwordLength;$i++){
$password.=$password_characters[rand(0,count($password_characters)-1)];
}
return $password;
}
echo generatePasswordsUsingCharactersAndNumbers(8);
?>
4. PHP class for generate multiple random passwords
This is a special php class that use to generate random passwords. You can generate any kind of passwords using this. It can create various types of passwords including numbers,characters, and special characters. Also you can manually set the special characters. You can generate unbreakable php passwords using this class. Please note before you save to password to the database, convert it to a hash value using one way encryption function. Do not save password directly.
<?php
/*
Copyright By http://www.latestcode.net
*/
class PHP_Password_Generator{
public $password_length;
public $password;
private $numbers;
private $alphabetic_chars;
private $special_chars;
private $password_type;
public function __construct($type){
$this->password_type = $type;
$this->numbers='0123456789';
$this->alphabetic_chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$this->special_chars='@#$%&!';
}
/*
Generate Password
*/
public function generate_password($length=8){
$this->password_length = ($length<1?8:$length);
$this->password='';
switch($this->password_type){
case 1:{//Numbers Only
$password_characters = $this->numbers;
}break;
case 2:{//Alphabetic CHaracters Only
$password_characters = $this->alphabetic_chars;
}break;
case 3:{//Numbers And Letters Only
$password_characters = $this->numbers.$this->alphabetic_chars;
}break;
case 4:{//Numbers,Letters, and Special Characters
$password_characters = $this->numbers.$this->alphabetic_chars.$this->special_chars;
}break;
default:{//Letters
$password_characters = $this->alphabetic_chars;
}break;
}
$password_characters = str_split($password_characters);
shuffle($password_characters);
for($i=0;$i<$this->password_length;$i++){
$this->password.=$password_characters[rand(0,count($password_characters)-1)];
}
return $this->password;
}
/*
Add custom special characters to the password
*/
public function set_special_characters($special_chars){
$this->special_chars = $special_chars;
}
}
//Example 1:
$pwd = new PHP_Password_Generator(4);//Generate password with numbers,letters, and special characters
echo $pwd->generate_password(10);//specify password length
//Example 2:
$pwd = new PHP_Password_Generator(4);
$pwd->set_special_characters('@#$%^&*');//Set custom special characters
echo $pwd->generate_password(10);
//Example 3:
$pwd = new PHP_Password_Generator(1);//Password will contain numbers
echo $pwd->generate_password(10);
//Example 4:
$pwd = new PHP_Password_Generator(4);//Make list of passwords
for($x=0;$x<5;$x++){
echo $pwd->generate_password(10).'<br/>';
}
?>