Created
December 10, 2023 18:18
-
-
Save adeel-raza/33c06bde74fa8fd12ba10bd8fdfd6236 to your computer and use it in GitHub Desktop.
OOP based password generator in PHP
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 | |
class Password_Manager { | |
private $length; | |
private $useUppercase; | |
private $useLowercase; | |
private $useDigits; | |
private $useSpecialChars; | |
private $password; | |
function generatePassword() { | |
if ( ! isset( $_POST['generate'] ) ) { | |
return; | |
} | |
$this->length = htmlspecialchars( isset( $_POST['length'] ) ? $_POST['length'] : 6 ); | |
$this->useUppercase = htmlspecialchars( isset( $_POST['uppercase'] ) ? $_POST['uppercase'] : false ); | |
$this->useLowercase = htmlspecialchars( isset( $_POST['lowercase'] ) ? $_POST['lowercase'] : false ); | |
$this->useDigits = htmlspecialchars( isset( $_POST['digits'] ) ? $_POST['digits'] : false ); | |
$this->useSpecialChars = htmlspecialchars( isset( $_POST['special'] ) ? $_POST['special'] : false ); | |
$formats = array( | |
'uppercase' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'lowercase' => 'abcdefghijklmnopqrstuvwxyz', | |
'digits' => '0123456789', | |
'special' => '!@#$%^&*()_+-=[]{}|;:,.<>?', | |
); | |
$selectedFormats = 0; | |
if ( $this->useUppercase ) { | |
$selectedFormats++; | |
} | |
if ( $this->useLowercase ) { | |
$selectedFormats++; | |
} | |
if ( $this->useDigits ) { | |
$selectedFormats++; | |
} | |
if ( $this->useSpecialChars ) { | |
$selectedFormats++; | |
} | |
if ( $selectedFormats === 0 ) { | |
return 'Please select at least one character format.'; | |
} | |
$this->password = ''; | |
$charactersPerFormat = floor( $this->length / $selectedFormats ); | |
while ( $this->length > 0 ) { | |
if ( $this->useUppercase && $this->length > 0 ) { | |
$shuffledStr = str_shuffle( $formats['uppercase'] ); | |
$randomSubset = substr( $shuffledStr, 0, $charactersPerFormat ); | |
$this->password .= $randomSubset; | |
$this->length = $this->length - $charactersPerFormat; | |
} | |
if ( $this->useLowercase && $this->length > 0 ) { | |
$shuffledStr = str_shuffle( $formats['lowercase'] ); | |
$randomSubset = substr( $shuffledStr, 0, $charactersPerFormat ); | |
$this->password .= $randomSubset; | |
$this->length = $this->length - $charactersPerFormat; | |
} | |
if ( $this->useDigits && $this->length > 0 ) { | |
$shuffledStr = str_shuffle( $formats['digits'] ); | |
$randomSubset = substr( $shuffledStr, 0, $charactersPerFormat ); | |
$this->password .= $randomSubset; | |
$this->length = $this->length - $charactersPerFormat; | |
} | |
if ( $this->useSpecialChars && $this->length > 0 ) { | |
$shuffledStr = str_shuffle( $formats['special'] ); | |
$randomSubset = substr( $shuffledStr, 0, $charactersPerFormat ); | |
$this->password .= $randomSubset; | |
$this->length = $this->length - $charactersPerFormat; | |
} | |
} | |
$this->password = str_shuffle( $this->password ); | |
return $this->password; | |
} | |
} | |
$password_manager = new Password_Manager(); | |
$generated_password = $password_manager->generatePassword(); | |
?> | |
<!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> | |
<h2>Password Generator</h2> | |
<form action="" method="post"> | |
<div class="form-group"> | |
<label for="length">Password Length:</label> | |
<input type="text" 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> | |
<!-- Template View --> | |
<h2>Generated Password:</h2> | |
<p><?php echo htmlspecialchars($generated_password); ?></p> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment