Created
May 25, 2011 10:17
-
-
Save rutger1140/990725 to your computer and use it in GitHub Desktop.
Basic single file password generator
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 | |
/** | |
* Password generator script | |
* Optimized with help from http://forrst.com/people/nicholasruunu | |
*/ | |
// Set default number of passwords to generate | |
$amount = isset($_GET['amount']) ? $_GET['amount'] : 5; | |
// Set default length | |
$length = isset($_GET['length']) ? $_GET['length'] : 8; | |
// Create random string with given length | |
// Uses ASCII table for speed improvement | |
function generatePassword($length){ | |
$length = intval($length); | |
$returnString = ''; | |
for ($count = 0; $count < $length; $count++) { | |
switch (mt_rand(0,3)) { | |
case 0: | |
$char = array(33, 35, 36, 37, 63, 64); // ! # $ % ? @ | |
$char = chr($char[array_rand($char)]); | |
break; | |
case 1: | |
$char = chr(mt_rand(97,122)); // a to z | |
break; | |
case 2: | |
$char = chr(mt_rand(48,57)); // 0 to 9 | |
break; | |
case 3: | |
$char = chr(mt_rand(65,90)); // A to Z | |
} | |
$returnString .= $char; | |
} | |
// Convert ASCII chars to HTML valid characters | |
return htmlentities($returnString); | |
} | |
?> | |
<html> | |
<head> | |
<title>Password generator</title> | |
<style type="text/css"> | |
*{ font-family:Helvetica,Arial,Verdana,sans-serif; } | |
p{ color:#444; } | |
input,button{ font-size:16px; } | |
small{ font-style:italic; } | |
li{ cursor:pointer;line-height:180%; } | |
li input{ font-family:monospace; } | |
</style> | |
</head> | |
<body> | |
<h1>Password generator</h1> | |
<p><a href="passwordgenerator.php">Reset to default</a></p> | |
<form method="get"> | |
<fieldset> | |
<legend>Generate passwords</legend> | |
<p> | |
Give me <input type="text" name="amount" size="2" value="<?php echo $amount ?>" /> | |
passwords with | |
<input type="text" name="length" size="2" value="<?php echo $length ?>" /> | |
characters. | |
<button type="submit">Generate</button> | |
</p> | |
<small>TIP: Click on number to select text</small> | |
</fieldset> | |
</form> | |
<ol> | |
<?php // Show passwords on screen | |
for($i = 0; $i < $amount; $i++) : ?> | |
<li><input value="<?php echo generatePassword($length) ?>" size="<?php echo $length ?>" /></li> | |
<?php endfor; ?> | |
</ol> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | |
<script> | |
// Click entire LI to select text | |
$(function() { | |
$('li').click(function(){ | |
$(this).find('input').select(); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment