+ 1

Need help badly in php file(); function

i have created a function which checks for username if it is present in users.txt (where each name is in a new line) file or not but the if condition $name == $userid isn't giving true value even for identical names. <?php //Check if user exists or not... $user = $_GET['userid']; $flag = $_GET['flag']; if($flag=='true'){checkuser($user);} function checkuser($userid){ echo 'chk user started with name '.$userid.'<br />'; //echo 'check user function started'; $userlist = file("users/users.txt"); foreach ($userlist as $name) { //getting problem in below line... if ($name == $userid) { if($flag!=='true'){return true;} else { echo "true"; //break; } } else {echo 'user not '.$name.' but '.$userid.'<br /> ';} } } ?> any help would be really appreciated thanks

3rd Jun 2018, 4:48 PM
Aman Kumar
Aman Kumar - avatar
7 Réponses
+ 4
Have you verified the contents of $userlist, did the file("users/users.txt"); succeeded, that the file exists and accessible? try to var_dump($userlist) to see its contents and maybe post the contents here, in general string comparison with == is sufficient (cmiiw)
3rd Jun 2018, 9:07 PM
Ipang
+ 3
Shouldn't it be $user == $name
3rd Jun 2018, 5:26 PM
Toni Isotalo
Toni Isotalo - avatar
+ 3
Aman Kumar, I'm glad you found the solution, IIRC trim() is used to crop whitespace (I guess you know that already) maybe there were whitespace in the names that prevents the comparison to work smoothly in the first place. Good job! and Thank you : )
4th Jun 2018, 9:09 AM
Ipang
+ 1
Ipang the contents are verified, around 8 names each in a new line. Btw this problem is sorted out now, got to do trim() don't know for what
4th Jun 2018, 9:02 AM
Aman Kumar
Aman Kumar - avatar
+ 1
Ipang i am still confused how that worked, as my files doesn't contain any white spaces and the line breaks or carriage return are already removed by the file() function as it reads each line into an array
4th Jun 2018, 2:39 PM
Aman Kumar
Aman Kumar - avatar
+ 1
Ipang i need some help in this problem too, already used trim but got nothing https://www.sololearn.com/Discuss/1324081/?ref=app
4th Jun 2018, 2:46 PM
Aman Kumar
Aman Kumar - avatar
0
Toni Isotalo stop spamming , read the code first, if u understand then give feedback, don’t bother with up-votes! thanks
3rd Jun 2018, 7:26 PM
Flash