How to Use PHP to Generate Random Passwords
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.
<!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.
<!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>