Page 1 of 2

Another Match: Because I can

Posted: Mon Jul 22, 2002 3:54 pm
by jason
Time Limit: None
Max Line Additions/Removals per Edit: 3
Restrictions: Backwards compatibility must be maintained. In other words, I should still be able to call random_password() and expect a 6 to 12 character password back, no matter what changes you make.

Code: Select all

<?php 
/* 
    string random_password ( void ) 
        Builds a random password 6 to 12 characters long 
*/ 
function random_password ()  
&#123;  
    $seed = (integer) md5(microtime());  
    mt_srand($seed);  
    $password = mt_rand(1,99999999); 
    $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));  
    return $password;  
&#125;  
?>

Posted: Mon Jul 22, 2002 5:38 pm
by Guest

Code: Select all

<?php 
/* 
    string random_password ( void ) 
        Builds a random password 6 to 12 characters long 
*/ 
function random_password ($seed = FALSE) // add optional seed-parameter
&#123;
    if (!$seed || (int)$seed == 0) // generate seed if parameter not set or not a number (or zero)
       $seed = (integer) md5(microtime());
    mt_srand($seed);
    $password = mt_rand(1,99999999);
    $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
    return $password;
&#125;  
?>

Posted: Mon Jul 22, 2002 5:39 pm
by volka
^ sorry forgot to log in :oops:

Posted: Tue Jul 23, 2002 5:35 am
by hex

Code: Select all

<?php
/*
    string random_password ( void )
        Builds a random password 6 to 12 characters long
*/
function random_password ($seed = FALSE) // add optional seed-parameter
&#123;
    if (!$seed || (int)$seed == 0) // generate seed if parameter not set or not a number (or zero)
        $seed = (integer) md5(microtime());
    mt_srand($seed);
    if ($dictfile = @file ("/usr/share/dict/words")) &#123;
        $rnd = rand(0, count($dictfile));
        return substr($dictfile&#1111;$rnd], mt_rand(0, 19), mt_rand(6, 12));
    &#125; else &#123;
        $password = mt_rand(1,99999999);
        $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12));
        return $password;
    &#125;
&#125;
?>

Posted: Tue Jul 23, 2002 7:58 am
by DSM

Code: Select all

<?php 
/* 
    string random_password ( void ) 
        Builds a random password 6 to 12 characters long 
*/ 
function random_password ($seed = FALSE) // add optional seed-parameter 
&#123; 
    if (!$seed || (int)$seed == 0) // generate seed if parameter not set or not a number (or zero) 
        $seed = (integer) md5(microtime()); 
    mt_srand($seed); 
    if ($dictfile = @file ("/usr/share/dict/words")) &#123; 
        $rnd = rand(0, count($dictfile)); 
        return substr($dictfile&#1111;$rnd], mt_rand(0, 19), mt_rand(6, 12)); 
    &#125; else &#123; 
        $password = mt_rand(1,99999999); 
        $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12)); 
        return $password; 
    &#125; 
&#125; 

//Form to generate password
function printForm()&#123;
print"<form action = '$PHP_SELF' method = 'post'>
<b>Generate Password</b>
<input type = 'submit' name = 'action' value = 'submit'></form>";
&#125;

?>

Posted: Tue Jul 23, 2002 9:45 am
by Guest

Code: Select all

<?php 
/* 
    string random_password ( void ) 
        Builds a random password 6 to 12 characters long 
*/ 
function random_password ($seed = FALSE)
&#123; 
    if (!$seed || (int)$seed == 0)
        $seed = (integer) md5(microtime()); 
    mt_srand($seed); 
    if ($dictfile = @file ("/usr/share/dict/words")) &#123; 
        $rnd = rand(0, count($dictfile));
        if(strlen($dictfile&#1111;$rnd]) < 32)  // reassure the string is long enough
           $dictfile&#1111;$rnd] .= md5(mt_rand(1,99999999));
        return substr($dictfile&#1111;$rnd], mt_rand(0, 19), mt_rand(6, 12)); 
    &#125; else &#123; 
        $password = mt_rand(1,99999999); 
        $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12)); 
        return $password; 
    &#125; 
&#125; 


function printForm()&#123; 
print"<form action = '$PHP_SELF' method = 'post'> 
<b>Generate Password</b> 
<input type = 'submit' name = 'action' value = 'submit'></form>"; 
&#125; 

?>

Posted: Tue Jul 23, 2002 9:49 am
by volka
sorry, forgot to login again :(
I just switched my IE6 to accept cookies only temporarily an forgot to add dnsprotect.com to the "always allow"-section - it's still devnetwork :roll:

Posted: Tue Jul 23, 2002 11:53 am
by protokol

Code: Select all

<?php 
/* 
    string random_password ( void ) 
        Builds a random password 6 to 12 characters long 
*/ 
function random_password ($seed = FALSE)
&#123; 
    if (!$seed || (int)$seed == 0)
        $seed = (integer) md5(microtime()); 
    mt_srand($seed); 
    if ($dictfile = @file ("/usr/share/dict/words")) &#123; 
        $rnd = rand(0, count($dictfile));
        if(strlen($dictfile&#1111;$rnd]) < 32)  // reassure the string is long enough
           $dictfile&#1111;$rnd] .= md5(mt_rand(1,99999999));
        return substr($dictfile&#1111;$rnd], mt_rand(0, 19), mt_rand(6, 12)); 
    &#125; else &#123; 
        $password = mt_rand(1,99999999); 
        $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12)); 
        return $password; 
    &#125; 
&#125; 

function printForm()&#123; 
print"<form action = '$PHP_SELF' method = 'post'> 
<b>Generate Password</b> 
<input type = 'submit' name = 'action' value = 'submit'></form>"; 
&#125; 

/*
<html><body>
<?php checkForm() ?>
</body></html>
*/
function checkForm() &#123;
   if ($_SERVER&#1111;'REQUEST_METHOD'] == 'GET'] &#123; printForm() &#125;
   elseif ($_SERVER&#1111;'REQUEST_METHOD'] == 'POST' && isset($_POST&#1111;'action'])) &#123; echo random_password() &#125;
&#125;
?>

Posted: Tue Jul 23, 2002 12:01 pm
by hex
Line 36 is more than 80 chars! volka wins!

Posted: Tue Jul 23, 2002 12:03 pm
by protokol
Well, where in this thread and in Jason's rules does it give this rule to follow? I don't see it anywhere. So maybe you are mistaken

Posted: Tue Jul 23, 2002 12:20 pm
by hex
heh. this is why we need the rules laid out... the original game that i played at sitepoint....
The VI Rule
Each line can be no more that 80 characters in width. Make sure you use a text editor that shows you character columns.
However, there are a few other rules we've overlooked as well... time to finalise the rules!

Posted: Tue Jul 23, 2002 12:23 pm
by protokol
That's fine then. Let's get some Official Rules going here .. maybe jason can make it a sticky announcement within the PHP Tennis forum

Posted: Tue Jul 23, 2002 12:28 pm
by llimllib
Post what you think here

Posted: Tue Jul 23, 2002 1:46 pm
by jason
Actually, I believe Hex's first rewrite was the loosing script, as it did not maintain BC. I tried it, and got no password returned to me. Can anyone back this up? Hehe

Either way, we need our own rules.

Posted: Tue Jul 23, 2002 3:35 pm
by volka
I think it has 2 faults
  • it doesn`t assure a 6-12 character long password (ok, depending on the dict-file)
  • $rnd = rand(0, count($dictfile)); may choose "the line after the last line"
but who am I to judge ? ;)


(hurray! been logged in on my first post :D )