Page 1 of 1

Match $var in text file ?

Posted: Wed Jul 01, 2009 12:05 pm
by Peuplarchie
Good day to you all,
stupid as it sound, all the thing that I have tried to match a variable into a text file and execute according to it result.

Code: Select all

 
 
$active = "user_list.txt";
$pos = strpos($active, $_SESSION['username']);
if ($pos === false) {
echo "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} else {
echo "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} 
 
 
 


Thanks !

Re: Match $var in text file ?

Posted: Wed Jul 01, 2009 12:11 pm
by Mark Baker
You're using strpos to search in the file name, not in the file itself.
You need to read the file content into a string variable, then search in that.

Code: Select all

 
$active = "user_list.txt";
$fileData = file_get_contents($active);
$pos = strpos($fileData, $_SESSION['username']);
if ($pos === false) {
echo "<b id=\"userinfo\"><img src=\"enligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} else {
echo "<b id=\"userinfo\"><img src=\"horsligne.png\" valign=\"bottom\" width=\"13px\"/></b>";
} 
 
Note that if you have a user called "Adam", and another called "Adam Smith", then searching for "Adam" in this way might return true because it matches "Adam Smith"