How to Generate Random Password with PHP and MySQL

Password is always be at the most risk because if your password is not strong then someone can steal your account and access all your details.Your password should have to be strong to prevent account stealing so it will be tougher to crack your password. So, in this tutorial we will show you how to generate strong random password using PHP and MySQL

To Generate Random Password It Takes Only three Steps:-

Step 1: Make a HTML file and define markup and scripting

We make a HTML file and save it with a name random_password.html

<html>
<head>
<link type="text/css" rel="stylesheet" href="random_password_style.css"/>
</head>
<body>
<div id="wrapper">

<div id="signup_form">
<p id="signup_label">Random Password Generator</p>
<form method="post"action="generate_password.php">
 <input type="text" name="name" id="name" placeholder="Enter Name">
 <br>
 <input type="text" name="email" placeholder="Enter Email">
 <br>
 <input type="submit" name="signup" value="DO SIGNUP">
</form>
</div>

</div>
</body>
</html>

In this step we create a form to enter details by user and send it to ‘generate_password.php’ file to generate password and do signup.Always validate data before form submitting you can view our form validation using jquery tutorial to see how to validate data.

Step 2: Make a PHP file to generate random password and do signup

We make a PHP file and save it with a name generate_password.php

// Database Structure 
CREATE TABLE `users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` text NOT NULL,
 `email` text NOT NULL,
 `password` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php
if(isset($_POST['signup']))
{
 $host="localhost";
 $username="root";
 $password="";
 $databasename="sample";
 $connect=mysql_connect($host,$username,$password);
 $db=mysql_select_db($databasename);

 $name=$_POST['name'];
 $email=$_POST['email'];
 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 $password = substr( str_shuffle( $chars ), 0, 8 );
 mysql_query("insert into users values('','$name','$email','$password')");
 echo "Your Password Is : ".$password;
}
?>

In this step we create a database table called ‘users’ to store user details and then get name and email entered by user and then generate random password by writting alphanumeric string and using str_shuffle() function to get strong and random password and after that we store all the details in table and display the password to user you can also send email to user email id having password in email message instead of displaying in form.

Step 3: Make a CSS file and define styling

We make a CSS file and save it with a name random_password_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#9FA3C9;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#4C3866;
}
#wrapper h1 p
{
 font-size:18px;
}
#signup_form
{
 background-color:white;
 width:270px;
 margin-left:345px;
 box-shadow:0px 0px 15px 0px #4C3866;
 padding:15px;
}
#signup_form #signup_label
{
 color:#4C3866;
 margin:0px;
 padding-bottom:10px;
 margin-bottom:30px;
 font-size:25px;
 border-bottom:1px solid #E6E6E6;
}
#signup_form input[type="text"]
{
 width:230px;
 height:35px;
 padding-left:10px;
 font-size:16px;
 margin-bottom:15px;
 border:1px solid #D8D8D8;
 color:#585858;
}
#signup_form input[type="submit"]
{
 width:230px;
 margin-left:-4px;
 height:40px;
 font-size:16px;
 font-weight:bold;
 background-color:#775E9B;
 color:white;
 border:none;
 border-bottom:5px solid #4C3866;
 border-radius:3px;
}

That’s all, this is how to generate random and strong password using PHP and MySQL.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Thạn you so much !

#PHP #HTML #MySQL

How to Generate Random Password with PHP and MySQL
10.50 GEEK