How to generate random password using PHP and MySQL

This tutorial concerns how to generate online secure and strong random password via PHP and to mail it to anybody’s email ID when they forgot their password. Also, once the user log in to his/her account using auto-generated password.

HTML Code

<html>
    <head>
    </head>
    <body>
        <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>
    </body>
</html>

PHP Code

<?php
if (isset($_POST['signup'])) {
    $name   = $_POST['name'];
    $email  = $_POST['email'];
    $chars  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $password = substr(str_shuffle($chars), 0, 8);
    mysqli_query($con,"insert into users (name,email,password) values('$name','$email','$password')");
    echo "Your Password Is : " . $password;
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *