PDA

View Full Version : Random Password Generator (my version)



Fellixombc
July 6th, 2010, 03:28
Well, Shaun posted a script which generates a random password, and I didn't like how it was structured and everything. So I went ahead made my own one. It's smaller.

Option #1:

<?php
function generate() {
$chars = 'abcdefghijklmnopqrstuvwxyz';
$nums = '123456790';
$pass = '';

for($i = 0; $i < rand(8, 13); $i++) {
$key = rand(1, 2);
if ($key == 1)
$pass.=$chars[rand (0, 25)];
else if ($key == 2)
$pass.=$nums[rand (0, 8)];
}

return $pass;
}
echo generate();
?>

Option #2:


<?php
function generate() {
$chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
$pass = '';

for($i = 0; $i < rand(8, 13); $i++)
$pass .= $chars[rand (0, 35)];
return $pass;
}
echo generate();
?>

Faab234
July 6th, 2010, 09:09
Nice job Fellixombc.

Justin H
July 6th, 2010, 23:19
Very good job there Fellix. :)

New Clear
July 6th, 2010, 23:34
So how do I use it?(;

Shishir G
July 7th, 2010, 00:04
I can do this in my head.
lol jk
kewl script.

Matt`
July 7th, 2010, 08:34
So how do I use it?(;

I dunno maybe type generate() or something l0L!

Faab234
July 7th, 2010, 13:35
So how do I use it?(;

It echoes the function called, Generate.

Justin H
July 7th, 2010, 15:12
Just......put it on your website? It's already echoing out a random password....and you can edit it to your settings. :P

Spear
July 7th, 2010, 15:14
Very nice.

Fellixombc
July 8th, 2010, 06:04
Updated. Option 2 is a lot smaller, just contains less numbers.

Pie`
July 11th, 2010, 00:40
Sorry but, I prefer my method:


function generate_pass($min_len=15) {
$pass = "";
while(strlen($pass) < $min_len) {
$pass .= (strlen($pass)%2==0)?rand(0,1)===1?chr(rand(65,90) ):chr(rand(97,122)):chr(rand(48,57));
}
return $pass;
}

Test script:

<?php
for($i=0;$i<20;$i++) {
echo generate_pass()."<br/>";
}
?>

output:

O3K1Z7U1w7k7n1Q
R8O4F5z3w4b0M8h
e7r4A5Y0X2h1T1G
n5g0E1M8x8Y2C6P
k2r9U4s3a4w2d9B
A2H1M6U1r4e5T5e
Y4P1J0B8e0F0n8n
x9O7e6B8T6M9z8L
O8O6j2G1u5w5O2u
i9Z7z0q3v1V8l6j
k4W4u6X8f4d1C6Y
f6k5S3D9r7v4i0U
F1j9P9l9U4E6f2i
k2b6H7t9q1x8N5P
n5n6w8g0J8N1Q7u
Z6W5C7O7s4P3Y2b
Z6V9y7W6g3C3Y8T
N7M4I0S6W5V6P1Z
q9a7U6i2P9N5V5u
G0r1C0T7J9A5b1C

Aaron
July 11th, 2010, 00:43
Very nice:)

jamie
July 11th, 2010, 10:03
Thanks this is helpful for me. (: