PDA

View Full Version : (PHP) Random Pass Generator Script



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>&bull; 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>&bull; Length input contained non-numerical characters.</em>";
elseif ($_REQUEST['value'] == '0')
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>&bull; Length input was submitted as 0.</em>";
elseif (empty($_REQUEST['value']))
$error = "
<strong>The following errors were processed during your request:</strong>
<br />
<em>&bull; 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 &quot;Generate Password&quot;!</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:&nbsp;&nbsp;<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>';

?>

Justin H
July 5th, 2010, 15:46
I'm pretty sure this is ripped, maybe?

Only the registered members can see the link.

Shaun
July 5th, 2010, 15:49
I'm pretty sure this is ripped, maybe?

Only the registered members can see the link.

Not it isn't ripped. Colette Brunel is me.

(I can prove it if you want).

Twisted
July 5th, 2010, 16:02
Not it isn't ripped. Colette Brunel is me.

(I can prove it if you want).

So your a chick?

Matt`
July 5th, 2010, 18:40
LOL pooned

Brendan
July 5th, 2010, 20:06
Not it isn't ripped. Colette Brunel is me.

(I can prove it if you want).

Prove it to myself in PM please, I'm closing this thread for the moment until you do.

Fellixombc
July 5th, 2010, 20:23
Moved to the web development section, this isn't a tutorial, and sorry for moving it while you were locking it Brendan.