2012-04-25 2 views
0

POST 메서드로 얻은 문자열과 .txt 파일의 문자열을 비교하고 싶습니다. 일치하는 동작이있는 경우 ... 나는 이것을 가지고 있지만 그것은 루프를 통과 할 수없는 것 같습니다 ... 그것은 전자 메일에 대한 일치를 검색하고 .txt의 모든 세 번째 문자열은 전자 메일 문자열이므로 이것이 세 번 반복되는 이유입니다 ...

+0

단지'in_array'를 사용하십시오. http://php.net/manual/en/function.in-array.php – mgraph

+3

아마도'$ i + 3' 대신'$ i + = 3'을 사용했을 것입니다. – anubhava

+0

그게 ... 고맙습니다. – ljencina77

답변

0
<?php 

/* content of emailList.txt 
user_1 password_1 [email protected] 
user_2 password_2 [email protected] 
user_3 password_3 [email protected] 
user_4 password_4 [email protected] 
user_999 password_999 [email protected] 
user_5 password_5 [email protected] 
*/ 

// set POST for testing only!! 
$_POST['user'] = '[email protected]'; 
$_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; 
    } 
} 

?> 
관련 문제