How to Use PHP to Generate Random Passwords

1 year ago admin PHP

In this tutorial, we will see how to use PHP to generate random passwords, so our application will generate a password based on requirements selected by the user he can include lowercase or uppercase characters, numbers, or symbols.


Create the form

First, we will create a form with options to select from the type of password the user wants to generate.

Form demo

                                                    
                                                                                                                
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Password Generator</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
  </head>
  <body class="bg-light">
    <div class="container">
        <div class="row my-5">
            <div class="col-md-8 mx-auto">
                <div class="card">
                    <div class="card-body">
                        <div class="card-title text-center">
                            <h3>Password Generator</h3>
                        </div>
                        <hr>
                        <div class="row mt-4">
                            <div class="col-md-6 mx-auto">
                                <form action="generatePassword.php" method="post">
                                    <div class="form-group row mt-3 d-flex align-items-center">
                                        <div class="col-md-8">
                                            <span class="fw-bold">
                                                Password Length:
                                            </span>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="number"     
                                                name="length" placeholder="Length" 
                                                min="6" max="20" required class="form-control border border-dark rounded-0">
                                        </div>
                                    </div>
                                    <div class="form-check my-2">
                                        <input class="form-check-input border border-dark rounded-0" 
                                            type="checkbox" name="uppercase" value="1">
                                        <label class="form-check-label" for="uppercase">
                                            Include Uppercase
                                        </label>
                                    </div>
                                    <div class="form-check my-2">
                                        <input class="form-check-input border border-dark rounded-0" 
                                            type="checkbox" name="numbers" value="1">
                                        <label class="form-check-label" for="numbers">
                                            Include Numbers
                                        </label>
                                    </div>
                                    <div class="form-check my-2">
                                        <input class="form-check-input border border-dark rounded-0" 
                                            type="checkbox" name="symbols" value="1">
                                        <label class="form-check-label" for="symbols">
                                            Include Symbols
                                        </label>
                                    </div>
                                    <hr>
                                    <div class="d-flex justify-content-center my-2">
                                        <button name="generate_password" class="btn btn-block btn-dark">
                                            Generate Password
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
  </body>
</html>

Create the file generatePassword.php

Next, we create a file 'generatePassword.php' Inside we receive the options the user selected and we generate and display the password.

Demo

                                                        
                                                                                                                        
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>PHP Password Generator</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ" crossorigin="anonymous">
  </head>
  <body class="bg-light">
    <div class="container">
        <div class="row my-5">
            <div class="col-md-8 mx-auto">
                <div class="card">
                    <div class="card-body">
                        <div class="card-title d-flex justify-content-between">
                            <h3>Your Password</h3>
                            <a href="index.php" class="btn btn-link">
                                Home
                            </a>
                        </div>
                        <hr>
                        <?php
                            if(isset($_POST['generate_password'])) {
                                $chars = 'abcdefghijklmnopqrstuvwxyz';
                                $password = '';

                                // Get requirements
                                if (isset($_POST['uppercase']) && $_POST['uppercase']) {
                                    $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
                                }
                                if (isset($_POST['numbers']) && $_POST['numbers']) {
                                    $chars .= '0123456789';
                                }
                                if (isset($_POST['symbols']) && $_POST['symbols']) {
                                    $chars .= '!@#$%^&*()-_';
                                }

                                $charCount = strlen($chars);

                                // Generate random password
                                for ($i = 0; $i < $_POST['length']; $i++) {
                                    $index = random_int(0, $charCount - 1);
                                    $password .= $chars[$index];
                                }

                                echo '<div class="d-flex justify-content-center"><span class="badge bg-dark text-white p-2">'.$password.'</span></div>';
                            }

                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
  </body>
</html>

Related Tuorials

CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...


How to Sort Ascending an Array in PHP

In this lesson, we will see how to sort ascending an array in PHP, we will use the sort() function t...


How to Remove a Key and its Value from an Associative Array in PHP

In this lesson, we will see how to remove a key and its value from an associative array in PHP,&nbsp...


How to Modify a Value in an Associative Array in PHP

In this lesson, we will see how to modify a value in an associative array in PHP, an Associative arr...