CodeIgniter Forums
Random letters from password check - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Random letters from password check (/showthread.php?tid=39708)



Random letters from password check - El Forum - 03-18-2011

[eluser]Total Shop UK[/eluser]
Random letters from password check

I developed the basic code below and thoguht others may find it useful.
I'm sure it can be improved so please be my guest.

Code:
<?php
$password = 'password';
$required_letters = 3;
$letters = array();
while (count($letters) < $required_letters){
    $k = rand(1, strlen($password));
    $letters[$k] = substr($password, $k-1, 1);
}
ksort($letters);
if ($_POST['letter1']!=''){
    $p=1;
    for ($i=0; $i <= $required_letters; $i++) {
        if ($password[$_POST['key'.$i]-1]==$_POST['letter'.$i]){
            // nothing here
        }else{
            $p=0;    
        }
    }
    if ($p==1){
        echo 'Password Correct!';
    }else{
        echo 'Password Incorrect!';
    }
}else{
    echo '&lt;form name="input" action="test.php" method="post"&gt;';
    $i=1;
    foreach ($letters as $key => $value) {
        echo 'Please Type Letter '.$key.': ';
        echo '&lt;input type="hidden" name="key'.$i.'" value="'.$key.'"&gt;';
        echo '&lt;input type="password" name="letter'.$i.'" maxlength="1" style="width:25px"&gt;&lt;br>';
        $i++;
    }
    echo '&lt;input type="submit" name="submit" value="submit"&gt;';
    echo '&lt;/form&gt;';
}
?&gt;