将带有$ _POST的字符串与从php中的file.txt获取的字符串进行比较

时间:2012-04-25 17:32:57

标签: php

我想将使用POST方法获得的字符串与.txt文件中的字符串进行比较...如果有匹配请执行操作...我得到了这个但是它似乎无法通过循环...它搜索匹配的电子邮件,.txt中的每个第三个字符串都是一个电子邮件字符串,这就是为什么迭代三个...

<?php

$email = $_POST['user'];
$password = $_POST['pass'];
$filename = 'C:\xampp\htdocs\www\zavrsni\emailList.txt';

if (($row = file_get_contents($filename)) != '') {
    $wordsArray = explode(' ', $row);
    for ($i=0; $i<sizeof($wordsArray); $i+3) {
        if (strcmp($wordsArray[$i], $email) == 0){
            //some action
            exit();
        }
    }
}
?>

1 个答案:

答案 0 :(得分:0)

<?php

/* content of emailList.txt
user_1 password_1 test1@default.com
user_2 password_2 test2@default.com
user_3 password_3 test3@default.com
user_4 password_4 test4@default.com
user_999 password_999 test999@default.com
user_5 password_5 test5@default.com
*/

// set POST for testing only!!
$_POST['user'] = 'test999@default.com';
$_POST['pass'] = 'test';
//
$email = $_POST['user'];
$email = strtolower($email);
$password = $_POST['pass'];
$filename = './emailList.txt';

// read entire file into an array, skipping empty lines and not adding return characters
$trimmed_file_array = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($trimmed_file_array AS $row) {
  //print $row.'<br>';
  list($f_user, $f_password, $f_email) = explode(' ', $row);
  $f_email = strtolower($f_email);
  if ($email === $f_email) {
    print 'found user: '.$f_user.' - password: '.$f_password.' - email: '.$f_email.'<br>';
    break;
  }
}

?>