Created
November 3, 2023 16:09
-
-
Save adeel-raza/9e44cf8d8e5770dc0e7a254885efbacb to your computer and use it in GitHub Desktop.
PHP based password generator program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function generatePassword( | |
$length, | |
$useUppercase, | |
$useLowercase, | |
$useDigits, | |
$useSpecialChars | |
) { | |
$formats = [ | |
"uppercase" => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | |
"lowercase" => "abcdefghijklmnopqrstuvwxyz", | |
"digits" => "0123456789", | |
"special" => '!@#$%^&*()_+-=[]{}|;:,.<>?', | |
]; | |
$selectedFormats = 0; | |
if ($useUppercase) { | |
$selectedFormats++; | |
} | |
if ($useLowercase) { | |
$selectedFormats++; | |
} | |
if ($useDigits) { | |
$selectedFormats++; | |
} | |
if ($useSpecialChars) { | |
$selectedFormats++; | |
} | |
if ($selectedFormats === 0) { | |
return "Please select at least one character format."; | |
} | |
$password = ""; | |
$charactersPerFormat = floor($length / $selectedFormats); | |
while ($length > 0) { | |
if ($useUppercase && $length > 0) { | |
$shuffledStr = str_shuffle($formats["uppercase"]); | |
$randomSubset = substr($shuffledStr, 0, $charactersPerFormat); | |
$password .= $randomSubset; | |
$length = $length - $charactersPerFormat; | |
} | |
if ($useLowercase && $length > 0) { | |
$shuffledStr = str_shuffle($formats["lowercase"]); | |
$randomSubset = substr($shuffledStr, 0, $charactersPerFormat); | |
$password .= $randomSubset; | |
$length = $length - $charactersPerFormat; | |
} | |
if ($useDigits && $length > 0) { | |
$shuffledStr = str_shuffle($formats["digits"]); | |
$randomSubset = substr($shuffledStr, 0, $charactersPerFormat); | |
$password .= $randomSubset; | |
$length = $length - $charactersPerFormat; | |
} | |
if ($useSpecialChars && $length > 0) { | |
$shuffledStr = str_shuffle($formats["special"]); | |
$randomSubset = substr($shuffledStr, 0, $charactersPerFormat); | |
$password .= $randomSubset; | |
$length = $length - $charactersPerFormat; | |
} | |
} | |
$password = str_shuffle($password); | |
return $password; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Coding Arena</title> | |
<link href="https://fonts.googleapis.com/css?family=Montserrat:400,500,600,700" rel="stylesheet"> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous"> | |
<style> | |
* { | |
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif; | |
} | |
body { | |
font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Oxygen-Sans", Ubuntu, "Cantarell", "Helvetica Neue", sans-serif; | |
background-color: #f2f2f2; | |
margin: 0; | |
padding: 0; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
height: 100vh; | |
} | |
.container { | |
background-color: #fff; | |
padding: 20px; | |
border-radius: 5px; | |
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | |
max-width: 600px; | |
} | |
h1, h2, h3 { | |
color: #333; | |
} | |
div { | |
background-color: #f5f5f5; | |
padding: 5px; | |
color: #4f4f4f; | |
border: 1px solid #ddd; | |
border-radius: 3px; | |
display: block; | |
} | |
/* Style input fields and buttons */ | |
/* Add more styles as needed */ | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>My Coding Arena</h1> | |
<p>The code shows here:</p> | |
<div> | |
<!-- Add your code below this line --> | |
<h2>Password Generator</h2> | |
<form action="" method="post"> | |
<div class="form-group"> | |
<label for="length">Password Length:</label> | |
<input type="number" class="form-control" name="length" id="length" min="6" required> | |
</div> | |
<div class="mt-3"></div> | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" name="uppercase" id="uppercase"> | |
<label class="form-check-label" for="uppercase">Include Uppercase Letters</label> | |
</div> | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" name="lowercase" id="lowercase"> | |
<label class="form-check-label" for="lowercase">Include Lowercase Letters</label> | |
</div> | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" name="digits" id="digits"> | |
<label class="form-check-label" for="digits">Include Digits</label> | |
</div> | |
<div class="form-check"> | |
<input type="checkbox" class="form-check-input" name="special" id="special"> | |
<label class="form-check-label" for="special">Include Special Characters</label> | |
</div> | |
<button type="submit" class="btn btn-primary" name="generate">Generate Password</button> | |
</form> | |
<div class="mt-3"></div> | |
<?php | |
if (isset($_POST["generate"])) { | |
$length = isset($_POST["length"]) ? intval($_POST["length"]) : 6; | |
$useUppercase = isset($_POST["uppercase"]); | |
$useLowercase = isset($_POST["lowercase"]); | |
$useDigits = isset($_POST["digits"]); | |
$useSpecialChars = isset($_POST["special"]); | |
$password = generatePassword( | |
$length, | |
$useUppercase, | |
$useLowercase, | |
$useDigits, | |
$useSpecialChars | |
); | |
echo "<h2>Generated Password:</h2>"; | |
echo "<p>$password</p>"; | |
} | |
?> | |
<!-- Add your code above this line --> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Psuedo code:
a) create variables to save password length, is uppercase, is lowercase, is digits, is special, and the password (empty)
b) check if the user has selected at least one of the formats and password length
c) create an array to save all uppercase, lowercase, digits, and special chars as array keys
d) create another variable called characters_in_bucket which is equal to password length divided by no of selected formats
e) floor the value of characters_in_bucket to get the correct value
f) loop until the password length is greater than zero
f.a) check if the user has selected is uppercase and then access the uppercase array key and randomly select the no.of characters_in_bucket
f.a.a) append the selected character to the password variable
f.a.b) subtract the password length with characters_in_bucket to keep track of the remaining characters needed
f.b) check if the user has selected is lower and password length is greater than zero
f.b.a) then access the lower array key and randomly select the no.of characters_in_bucket
f.b.b) append the selected character to the password variable
f.b.c) subtract the password length with characters_in_bucket to keep track of the remaining characters needed
f.c) check if the user has selected is digits and and password length is greater than zero
f.c.a) then access the digits array key and randomly select the no.of characters_in_bucket
f.c.b) append the selected character to the password variable
f.c.c) subtract the password length with characters_in_bucket to keep track of the remaining characters needed
f.d) check if the user has selected is special and the password length is greater than zero
f.d.a) then access the special array key and randomly select the no.of characters_in_bucket
f.d.b) append the selected character to the password variable
f.d.c) subtract the password length with characters_in_bucket to keep track of the remaining characters needed
g) return the generated password variable back