Shaun
July 4th, 2010, 10:14
I've enhanced my script a bit and included the "form" that you all are asking for. I also added a built-in error handling system and options to use numbers, upper case letters, lower case letters, and symbols.
<?php
// Password Generator v1.2
// Created by Shaun
$limit = 100;
function generate_pass()
{
global $limit;
$pass = '';
$length = '';
$nums = array();
$caps = array();
$lowers = array();
$symbols = array();
if ($_REQUEST['usenums'])
$nums = range('0', '9');
if ($_REQUEST['usecaps'])
$caps = range('A', 'Z');
if ($_REQUEST['uselower'])
$lowers = range('a', 'z');
if ($_REQUEST['usesymbols'])
$symbols = array_merge(range("{", "~"), range(":", "@"));
$chars = array();
if (!empty($nums) )
{
foreach ($nums as $value)
$chars[] = $value;
}
if (!empty($caps) )
{
foreach ($caps as $value)
$chars[] = $value;
}
if (!empty($lowers) )
{
foreach ($lowers as $value)
$chars[] = $value;
}
if (!empty($symbols) )
{
foreach ($symbols as $value)
$chars[] = $value;
}
if (empty($chars))
$chars = array_merge(array_merge(range('a', 'z'), range('A', 'Z')), range('0', '9'));
$count = count($chars);
if( is_numeric($_REQUEST['value']) && (abs($_REQUEST['value']) < $limit) && (!empty($_REQUEST['value'])) )
$length = abs((int) $_REQUEST['value']);
elseif (empty($_REQUEST['value']) && ($_REQUEST['value'] != '0') && !isset($_REQUEST['value']))
$length = 'keepalive';
else
show_error();
mt_srand((double)microtime() * 1000000);
if ($length != 'keepalive')
{
for($i = 0; $i < $length; $i++)
$pass .= $chars[mt_rand(0, $count - 1)];
}
if (!empty($pass))
echo "Your password is:<br /><tt><strong>$pass</strong></tt>";
}
function show_error()
{
global $limit;
if ($limit <= abs($_REQUEST['value']))
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input is over or equal to $limit characters.</em>";
elseif( !is_numeric($_REQUEST['value']) && (!in_array($_REQUEST['value'], array('', '0', 'keepalive'))) )
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input contained non-numerical characters.</em>";
elseif ($_REQUEST['value'] == '0')
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input was submitted as 0.</em>";
elseif (empty($_REQUEST['value']))
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input was left empty.</em>";
else
$error = "
<strong>An error processed with your request.</strong>";
echo $error;
return;
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "Only the registered members can see the link.">
<html xmlns="Only the registered members can see the link."><head>
<meta Only the registered members can see the link."Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="author" content="ccbtimewiz" />
<title>Password Generator</title></head>';
if (!isset($_REQUEST['usenums']) && !isset($_REQUEST['usecaps']) && !isset($_REQUEST['uselower']) && !isset($_REQUEST['usesymbols']))
{
$_REQUEST['usenums'] = true;
$_REQUEST['usecaps'] = true;
$_REQUEST['uselower'] = true;
$_REQUEST['usesymbols'] = false;
}
if (!isset($_REQUEST['value']))
$keepalive = true;
echo '
<body>
<div align="left" style="padding: 2px;">
<span style="font-size: 140%">Password Generator</span><br />
<span style="font-size: 80%">Want a random password? Just fill out this form and click "Generate Password"!</span><br /><br />
<form action="' . $_SERVER['SCRIPT_NAME'] . '" method="post">
<label><input type="checkbox" name="usenums" ' , ($_REQUEST['usenums'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include numbers?</label>
<br />
<label><input type="checkbox" name="usecaps" ' , ($_REQUEST['usecaps'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include capital letters?</label>
<br />
<label><input type="checkbox" name="uselower" ' , ($_REQUEST['uselower'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include lowercase letters?</label>
<br />
<label><input type="checkbox" name="usesymbols" ' , ($_REQUEST['usesymbols'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include symbols/special characters?</label>
<br />
<label>Password length: <input type="text" name="value" value="' . (!empty($_REQUEST['value']) ? $_REQUEST['value'] : '') . '" maxlength="3" size="5" /></label><br />
<br />
<input value="Generate Password" type="submit" />
</form><br />';
generate_pass();
echo '
</div>
</body>
</html>';
?>
<?php
// Password Generator v1.2
// Created by Shaun
$limit = 100;
function generate_pass()
{
global $limit;
$pass = '';
$length = '';
$nums = array();
$caps = array();
$lowers = array();
$symbols = array();
if ($_REQUEST['usenums'])
$nums = range('0', '9');
if ($_REQUEST['usecaps'])
$caps = range('A', 'Z');
if ($_REQUEST['uselower'])
$lowers = range('a', 'z');
if ($_REQUEST['usesymbols'])
$symbols = array_merge(range("{", "~"), range(":", "@"));
$chars = array();
if (!empty($nums) )
{
foreach ($nums as $value)
$chars[] = $value;
}
if (!empty($caps) )
{
foreach ($caps as $value)
$chars[] = $value;
}
if (!empty($lowers) )
{
foreach ($lowers as $value)
$chars[] = $value;
}
if (!empty($symbols) )
{
foreach ($symbols as $value)
$chars[] = $value;
}
if (empty($chars))
$chars = array_merge(array_merge(range('a', 'z'), range('A', 'Z')), range('0', '9'));
$count = count($chars);
if( is_numeric($_REQUEST['value']) && (abs($_REQUEST['value']) < $limit) && (!empty($_REQUEST['value'])) )
$length = abs((int) $_REQUEST['value']);
elseif (empty($_REQUEST['value']) && ($_REQUEST['value'] != '0') && !isset($_REQUEST['value']))
$length = 'keepalive';
else
show_error();
mt_srand((double)microtime() * 1000000);
if ($length != 'keepalive')
{
for($i = 0; $i < $length; $i++)
$pass .= $chars[mt_rand(0, $count - 1)];
}
if (!empty($pass))
echo "Your password is:<br /><tt><strong>$pass</strong></tt>";
}
function show_error()
{
global $limit;
if ($limit <= abs($_REQUEST['value']))
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input is over or equal to $limit characters.</em>";
elseif( !is_numeric($_REQUEST['value']) && (!in_array($_REQUEST['value'], array('', '0', 'keepalive'))) )
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input contained non-numerical characters.</em>";
elseif ($_REQUEST['value'] == '0')
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input was submitted as 0.</em>";
elseif (empty($_REQUEST['value']))
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>• Length input was left empty.</em>";
else
$error = "
<strong>An error processed with your request.</strong>";
echo $error;
return;
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "Only the registered members can see the link.">
<html xmlns="Only the registered members can see the link."><head>
<meta Only the registered members can see the link."Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="author" content="ccbtimewiz" />
<title>Password Generator</title></head>';
if (!isset($_REQUEST['usenums']) && !isset($_REQUEST['usecaps']) && !isset($_REQUEST['uselower']) && !isset($_REQUEST['usesymbols']))
{
$_REQUEST['usenums'] = true;
$_REQUEST['usecaps'] = true;
$_REQUEST['uselower'] = true;
$_REQUEST['usesymbols'] = false;
}
if (!isset($_REQUEST['value']))
$keepalive = true;
echo '
<body>
<div align="left" style="padding: 2px;">
<span style="font-size: 140%">Password Generator</span><br />
<span style="font-size: 80%">Want a random password? Just fill out this form and click "Generate Password"!</span><br /><br />
<form action="' . $_SERVER['SCRIPT_NAME'] . '" method="post">
<label><input type="checkbox" name="usenums" ' , ($_REQUEST['usenums'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include numbers?</label>
<br />
<label><input type="checkbox" name="usecaps" ' , ($_REQUEST['usecaps'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include capital letters?</label>
<br />
<label><input type="checkbox" name="uselower" ' , ($_REQUEST['uselower'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include lowercase letters?</label>
<br />
<label><input type="checkbox" name="usesymbols" ' , ($_REQUEST['usesymbols'] && !$keepalive) ? 'checked="checked"' : '' , ' /> Include symbols/special characters?</label>
<br />
<label>Password length: <input type="text" name="value" value="' . (!empty($_REQUEST['value']) ? $_REQUEST['value'] : '') . '" maxlength="3" size="5" /></label><br />
<br />
<input value="Generate Password" type="submit" />
</form><br />';
generate_pass();
echo '
</div>
</body>
</html>';
?>