[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 '<form name="input" action="test.php" method="post">';
$i=1;
foreach ($letters as $key => $value) {
echo 'Please Type Letter '.$key.': ';
echo '<input type="hidden" name="key'.$i.'" value="'.$key.'">';
echo '<input type="password" name="letter'.$i.'" maxlength="1" style="width:25px"><br>';
$i++;
}
echo '<input type="submit" name="submit" value="submit">';
echo '</form>';
}
?>