PDA

View Full Version : [PHP]Random Password Generator..



Jme
19-02-2008, 10:31 PM
Having some trouble generating the random pass, Anyone have any ideas?


<?php
function random_pass($length)
{
$chars = "abcdefghijkmnopqrstuvwxyz1234567890";
$pass = "";
$charlength = strlen($chars);
$randchar = rand()%($charlength);
while(0<$length)
{
$pass = substr($chars, $randchar, 1);
}
return($pass);
}
$password = random_pass(7);
echo("" . $password . "");
?>

That's just a prototype to test the function.
It's basically meant to loop through until there are 7 characters in the pass string but it keeps giving the error:


Fatal error: Maximum execution time of 30 seconds exceeded in /home/jclabz/public_html/pass.php on line 10

and


Fatal error: Maximum execution time of 30 seconds exceeded in /home/jclabz/public_html/pass.php on line 8

Been trying to debug it for about an hour starting to annoy me alot now.

Hypertext
19-02-2008, 10:44 PM
Sounds like it's taking too long to create the 7 chars, you could just make a class ;)

RYANNNNN
19-02-2008, 10:46 PM
set_timeout_limit(0);

Jme
19-02-2008, 11:05 PM
set_timeout_limit(0);
You mean set_time_limit?
That doesn't do anything cept change the timeout from 30 to 0?

Baving
19-02-2008, 11:14 PM
<?php
function random_pass($length)
{
$chars = "abcdefghijkmnopqrstuvwxyz1234567890";
$pass = "";
$charlength = strlen($chars);

$i=0;
while($i<$length)
{
$randchar = rand()%($charlength);
$pass = substr($chars, $randchar, 1);
$i++;
}
return($pass);
}
$password = random_pass(7);
echo("" . $password . "");
?>

Jme
19-02-2008, 11:20 PM
<?php
function random_pass($length)
{
$chars = "abcdefghijkmnopqrstuvwxyz1234567890";
$pass = "";
$charlength = strlen($chars);

$i=0;
while($i<$length)
{
$randchar = rand()%($charlength);
$pass = substr($chars, $randchar, 1);
$i++;
}
return($pass);
}
$password = random_pass(7);
echo("" . $password . "");
?>

Genius haha I think it was the incrementer that was needed for it to function properly :] i had to add a . on $pass .=""; so it would add to it rather than replace it but cheers for sorting that out for me :]

Want to hide these adverts? Register an account for free!