0

我想将使用 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();
        }
    }
}
?>
4

1 回答 1

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;
  }
}

?>
于 2012-04-25T18:08:42.763 回答