How to generate random password using PHP and MySQL
Today, we will learn how to generate random password using PHP and MySQL. In this article, we will show you how to generate random passwords from the given specific string.
Generate Random Password Using PHP And MySQL, How to generate simple random password in php, A user friendly, strong password generator PHP function, How To Generate a Random String using PHP, Simple way to generate random password using php, Generating Random String Using PHP, auto generate password in php code, auto generate password in php code,php generate random string, generate 6 digit unique random number in php, php generate unique number sequence, how to create a random password generator in php.
Checkout more articles on PHP
- Convert CSV to JSON in PHP
- How to use session in PHP
- Get keys from an associative array in PHP
- Sorting Arrays in PHP
- Remove the last character from a string in PHP
Sometimes it’s required to auto generate secure passwords for user registration or in some other place. So we will show you how to do it.
Steps to generate random password
1. Create signup form
Here, we will create a file named `signup.php` for simple signup form. Also we have to pass another file in the action of the form.
signup.php
<html>
<head>
<title>Simple Signup Form</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div id="container">
<h2>Generator Random Password </h2>
<form method="post" action="generate_password.php">
<input type="text" name="name" class="form-control" placeholder="Enter Name" placeholder="Enter Name">
<br>
<input type="text" name="email" class="form-control" placeholder="Enter Email">
<br>
<input type="submit" name="signup" value="DO SIGNUP">
</form>
</div>
</body>
</html>
2. PHP code to generate random password
Now we will create a file named `generate_password.php` and write PHP code for data connection, create function to generate random password and encrypt the generated password to save into database.
generate_password.php
<?php
// database connection
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'test';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass , $db) or die($conn);
// create function for generate random password
function generate_password($len = 8){
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$password = substr( str_shuffle( $chars ), 0, $len );
return $password;
}
// insert into database
if(isset($_POST['signup'])) {
$name=$_POST['name'];
$email=$_POST['email'];
$password = generate_password();
$encpt_password= sha1($password);
mysqli_query($conn, "insert into registration (name, email, password) values ('$name', '$email', '$encpt_password')");
echo "Your Password Is : ".$password;
}
?>
// Output: Your password is: 4GQnHTN8
That’s it for today.
Thank you for reading. Happy Coding!