Sunday, December 30, 2012

PHP Random password generator

How to generate random password using PHP?

The bellow example shows you how to create random passwords in PHP
When you are talking about internet you can't forget security. Basically internet security depends on passwords. Why do you need passwords? Simply because protect your personal data from others. Passwords help you to keep your data secure. Without knowing the password anyone can't read or access your data. When we talking about passwords you have to consider the strength of the password.

Strong password should be,

  1. At least eight characters long.
  2. Very good combination of letters, numbers, and special characters.
  3. Don't use dictionary words.
  4. Don't use your phone number,reverse order of your name,nick name,NIC number,Passport number, or mobile number.
  5. A good combination of simple and capital letters.
  6. Don't use characters or numbers only.
  7. Memorized easily without save it to a other media.
  8. 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.

Complete source code for populate random password in PHP.

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

Complete PHP source code for generate password with letters.

<?php
function
generatePasswordsUsingCharacters($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.

Copy and paste bellow code populate strong password in PHP

<?php
function
generatePasswordsUsingCharactersAndNumbers($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.

Example code for create multiple random password list using PHP.

<?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/>';
}
?>

40 comments:

Anonymous said...

Có làn da trắng là mong ước của mọi cô gái , sản phẩm thuoc uong lam trang da ivory caps giúp thuốc uống trắng da toàn thân ngoài ra bạn cũng có thể sử dụng các loại kem chong nang tot nhat. Ngoài ra nếu bạn muốn làn da luôn tươi trẻ thì nên dùng my pham sakura nhat ban như kem duong da chong lao hoa sakura giúp làn da luôn trẻ đẹp xóa các nếp nhăn. Cách thuốc herba vixmen an toàn và hiệu quả bằng herba vixmen , vậy thuoc herba vixmen co tot khong , có an toàn không và mua ở đâu sẽ được cho biết sau đây.

Muatotthuoc said...

Bạn đang đau đầu vì thừa cân, bạn đang muốn tìm kiếm một giải pháp hiệu quả nhưng không làm tổn hại đến sức khỏe. Xin mời bạn đọc bài viết 6 loại thuốc giảm cân hiệu quả nhất hiện nay. Để có cái nhìn rõ hơn về các loại thuốc giảm cân nhé.
Ngoài ra Muathuoctot của chúng tôi còn cung cấp rất nhiều thực phẩm chức năng như dong trung ha thao, kem chống nắng tốt nhất hiện nay, thuốc uống trắng da có an toàn, thuốc mọc lông mày ,kem tri tham quang mat ,thuốc sung dha prenatal vitamins cho bà bầu ,vien uong collagen
Vào những tháng hè, mọi người thường có nhu cầu đi biển du lịch nhưng lại sợ làm đen sạm da. Nên thường có nhu cầu tìm hiểu về các loại kem chống nắng tốt, không gây kích ứng da. Mời các bạn đọc thêm bài viết Mua kem chống nắng loại nào tốt để bổ sung thêm vào hành trang du lịch của mình nhé.

Unknown said...

Kem xoài được khá nhiều người yêu thích vì nó ít ngọt hơn các loại kem khác. Dưới đây là 3 cach lam kem xoai
Sau đây chuyên mục món ăn ngon xin hướng dẫn cách làm sữa chua từ sữa tươi, cách làm sữa chua từ sữa đặc ngon tại nhà. Với những hướng dẫn chi tiết sau. Mong rằng sẽ cung cấp cho bạn được cách làm sữa chua ngon
Đứa trẻ nào cũng bị “hút hồn” bởi “ma lực” của kem chuối, ngay cả người lớn cũng vậy. Vậy cách làm kem chuối thơm ngon như thế nào để ôtủ lạnh nhà bạn luôn có món kem chuối khoái khẩu?Sau đây chúng tôi chia sẻ cách làm kem chuối
Không chỉ là một loại dầu đa năng, phát huy tốt tác dụng trong việc chăm sóc sức khỏe, chế biến các món ăn hàng ngày, dầu dừa còn có công dụng làm đẹp da rất hiệu quả mà không phải ai cũng biết. Chính vì vậy, trong thời gian gần đây, rất nhiều chị em trở nên “cuồng” loại mỹ phẩm tự nhiên này nhờ hiệu quả làm đẹp vượt trội mà nó mang lại. Vậy dưỡng da bằng dầu dừa
Tác dụng của dầu dừa trong việc làm đẹp rất tuyệt với và được rất nhiều chị em phụ nữ tin dùng. Tuy nhiên, không phải ai cũng nắm được cách làm và tác dụng của loại thần dược này, hãy cùng khám phá cách làm đẹp từ dầu dừa
Dầu dừa là một loại thảo dược vô cùng tốt cho sức khóe,cũng như công cuộc làm đep của chị em phụ nữ.Nó được làm từ hoàn toàn tự nhiên,không độc hại,nhưng cách dùng dầu dừa
Dầu dừa là dược phẩm rất tuyệt vời để làm đẹp da, hạn chế rạn da, nám da hay tắm trắng. Bạn hoàn toàn có thể tự làm dầu dừa tại nhà để có được dầu dừa nguyên chất cho nhu cầu của mình. Chúng tôi sẽ hướng dẫn 2 cach lam dau dua

Anu said...


I’ve been browsing on-line greater than three hours today, but I never discovered any attention-grabbing article like yours. It is beautiful worth sufficient for me. Personally, if all webmasters and bloggers made good content material as you did, the net will be a lot more helpful than ever before.
iOS Training in Chennai
Android Training in Chennai
php Training in Chennai

Priya Kannan said...

That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
PHP Training in Chennai

Sandilyan said...

try this simple code : http://www.sanwebcorner.com/2017/08/generate-password-to-textbox-using-php.html

Unknown said...


Grate blog to read its very nice explanation about Internet Of Things
Austere Technologies |Internet Of Things

Austere said...

VERY INFORMATIVE BLOG. KEEP SHARING SUCH A GOOD ARTICLES.

Best Software Company in USA | Austere Technology Solutions

Unknown said...

Nice blog with excellent information. Thank you, keep sharing.

Best Software Security Services | Austere Technology Solutions

Deepika said...

Really great blog.

Best Cloud Services | Austere Technology Solutions

Austere said...

wow...nice blog, very helpful information. Thanks for sharing.

Best Quality Management Services | Austere Technology Solutions

Austere said...

Very good informative article. Thanks for sharing such nice article, keep on updating such good articles.

Best Internet of Things Services | Best IOT Services | Austere Technology Solutions

Unknown said...

Needed to compose one little word yet thanks for the suggestions that you are contributed here, would like to read this blog regularly to get more updates...
Best Online Software Training Institute | PHP Training

Deepika said...

Good information.

chartered accountant coaching in Hyderabad | Avinash College of commerce

Deepika said...

Wow...Excellent informative blog, really helpful. Thank you.

Best CMA Training in hyd | ISFS

Deepika said...

Excellent informative blog, keep for sharing.

Best System Integration services | Massil Technologies

Deepika said...

Best informative blog. Thank you.

cima courses in hyderabad | ISFS

Deepika said...

Your article gives lots of information to me. Thanks for sharing.
acca course in hyderabad | ISFS

Sathyatech said...

Article found very useful, thank you for sharing…

Php Training | Software Training courses in hyderabad

jagadeesh said...

best article with nice information thank you
DevOps Training in Hyderabad
Salesforce Training in Hyderabad
SAP ABAP Online Training
SEO Training in Hyderabad

jothikumar said...

It's really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing...
Selenium online training
Selenium certification training
Selenium online course
Selenium training course

nash b said...

Nice...
bitwise aptitude questions

how to hack flipkart legally

zenq interview questions

count ways to n'th stair(order does not matter)

zeus learning subjective test

ajax success redirect to another page with data

l&t type 2 coordination chart

html rollover image

hack android phone using cmd

how to hack internet speed upto 100mbps

nash b said...

GOOD...
internships in chennai

winter internship mechanical engineering

internship for aeronautical engineering students in india 2019

kaashiv

list of architectural firms in chennai for internship

paid internships in pune for computer science students

diploma final year project topics for information technology

internship

data science internship report

inplant training

meritstep Technology said...


Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
angular js online training
best angular js online training
top angular js online training

meritstep Technology said...

Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
workday studio online training
best workday studio online training
top workday studio online training

Data science said...

Thanks for sharing this interesting information. I am so happy to read this. I love reading this type of blog, again thank you for sharing.
Data Science Training in Hyderabad

Hadoop Training in Hyderabad

Java Training in Hyderabad

Python online Training in Hyderabad

Tableau online Training in Hyderabad

Blockchain online Training in Hyderabad

informatica online Training in Hyderabad

devops online Training

un known said...

I just loved your article on the beginners guide to starting a blog.If somebody take this blog article seriously in their life, he/she can earn his living by doing blogging.thank you for thizs article. top sap hana online training

riyaa said...

excellent...!!!
Selenium training in chennai

Industrial visit in chennai

Internship

Internships in bangalore for cse students 2019

Free internship in chennai for cse students

Network security projects for cse

Ccna course in chennai

Inplant training in chennai for cse

Inplant training for eee students

Kaashiv infotech chennai

shreeya karan said...

Nice blog!!!
Internships in pune for computer science students
Internships in pune for computer science students
Inplant training certificate format
Internships in bangalore for ece students
Industrial training for electronics and communication engineering students
Internship for computer science students in bangalore
Internship for ece students
Mba internship in chennai
Inplant training in chennai for ece
Internship in nagpur for cse

vijayshri said...

Thanks for sharing it.I got Very valuable information from your blog.your post is really very Informative.I’m satisfied with the information that you provide for me.Nice post. By reading your blog, i get inspired and this provides some useful information.

Php training in pune at 3ri Technologies

sam said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Regards : Selenium Training Institute in Pune

3riTec said...

Really Good article.provided a helpful information.keep updating...
Regards : Best SAP FICO Training in Pune With Placement

rohiniseema said...

very nice blogger thanks for sharing......!!!
coronavirus update
inplant training in chennai
inplant training
inplant training in chennai for cse
inplant training in chennai for ece
inplant training in chennai for eee
inplant training in chennai for mechanical
internship in chennai
online internship



Rajesh said...

It's very useful blog post with inforamtive and insightful content and i had good experience with this information.
Selenium Training in chennai | Selenium Training in annanagar | Selenium Training in omr | Selenium Training in porur | Selenium Training in tambaram | Selenium Training in velachery

shankarjaya said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Salesforce Training in Chennai | Certification | Online Course | Salesforce Training in Bangalore | Certification | Online Course | Salesforce Training in Hyderabad | Certification | Online Course | Salesforce Training in Pune | Certification | Online Course | Salesforce Online Training | Salesforce Training

My first blogs said...

I liked your article, Thanks for sharing,
Best Dot Net Institute in Pune
Best Market Research Institute in Pune
Best Data Science Institute in Pune
Best PHP Institute in Pune
Best Software Testing Institute in Pune
Best Clinical Research Courses in Pune
Best Digital Marketing Institute in Pune
Best Training Institute in Pune

Paras Arora said...

Really awesome blog!!! I finally found a great post here.I really enjoyed reading this article. Thanks for sharing valuable information.
Register for a free Online Demo Classes

Data Science Training Institute in Pune
Best Python Online Training
Online AWS Training
Online Data Science Training

Sarthak Yadav said...

Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts likethis. https://www.3ritechnologies.com/course/angular-js-training-institute-in-pune/

ACE said...

Thank you for sharing this useful article with us. This blog is a very helpful to me in future. Keep sharing informative articles with us.

https://www.ahmedabadcomputereducation.com/course/php-training-course/

Suryaprakash said...

I found some useful information from this article, thanks for sharing the useful information.
devops practices
uses of java programming language
what is campaign in google adwords
is web design a good career
seo executive interview questions