Question

I am not sure why this doesn't work:

<?PHP
$user = $_GET['user'];
$write = $user . "\n";
if(strpos(file_get_contents("recent.txt"),$write) !== false){
    file_put_contents('recent.txt', $write, FILE_APPEND | LOCK_EX);
}
?>

What I am doing is to get the search term from a form and write it to a file. The way the original code is set up, it is only possible (as of 2014) to have 14,530,014 entries.

When the form is submitted (or ?user=SomeUser is added to the URL), nothing happens to recent.txt. Any help would be appreciated.

Was it helpful?

Solution

If you want to write the info if it is not contained in the file, you need to change

strpos(file_get_contents("recent.txt"),$write) !== false

to

strpos(file_get_contents("recent.txt"),$write) === false

!== false means that the file contents do contain the user you were looking for. It would return false if the string was not found.

Additionally, you may want to add a call to isset() to check if $_GET['user'] is actually set.

And depending on how large you expect this file to get, using a database to do this may be a much better (more efficient) option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top