-2

I have a file, online.html. The file is supposed to store the names of all the current users who are online on the site. The document might look something like this:

<p>python-b5</p>
<p>Other user</p>

They are added with code like this:

$fp = fopen("online.html", 'a');
fwrite($fp, "<p>" . $_SESSION ['name'] . "</p>");
fclose($fp);

When the user logs out, I would like to delete the entry in online.html for their user. However, I can't figure out how to accomplish it. How would I delete the entry?

python-b5
  • 73
  • 2
  • 8
  • 3
    Possible duplicate of [How to delete a line from the file with php?](https://stackoverflow.com/questions/5712878/how-to-delete-a-line-from-the-file-with-php) – Dawid Zbiński May 22 '17 at 19:49
  • 3
    Use a database. – Pedro Lobito May 22 '17 at 19:50
  • You should just open file stream, parse it with `file_get_content`, then find the user you're looking for by using e.g. `stripos` on it's content and delete the line using the solution in "possible duplicate" question – Dawid Zbiński May 22 '17 at 19:53
  • Nobody will ever use this in a production environment? – Code4R7 May 22 '17 at 19:54
  • And also as it was pointed out by Pedro. You should definitely use a database for this kind of operations. You can just then sleect the date from the database and there is no need to write anything into the file. – Dawid Zbiński May 22 '17 at 19:54
  • just tried the answers over there, they didn't work – python-b5 May 22 '17 at 19:57
  • 2
    This is a pretty bad idea. You will need to lock the file, then parse it, write all but the line you want to remove back to the file. Then unlock it. If you don't lock and unlock you are going to have issues if two users login or out at the same time. Be sure to also lock it while writing the file. PHP has access to several databases, this is exactly the type of situation those are made to handle properly. – Eric Hodges May 22 '17 at 20:03
  • 1
    Pretty bad idea. In essence you're trying to recreate a database into a static html file. I'd strongly suggest to look at one of the database options that php provides and use that instead. Probably best to learn to have something simple: PDO (that's going to work toward any database you pick later just as well), and SQLite under it (I think it's the simplest form. That will allow you to add and remove records, and without having to code around concurrence etc. –  Nov 24 '18 at 01:34
  • How are users identified? Is there some sort of ID? – ryantxr Nov 24 '18 at 02:16

2 Answers2

1

The idea of using an HTML file as a database will have serious problems. There are two big problems.

  1. Making sure that only one process updates the file at any given time. If more than one process updates the file at the same time, the file will get corrupted or it won't record all the updates.

  2. Finding the right line to delete. You will need a way to unambiguously identify the line to delete.

Assuming you have some sort of user id, you could put that into the html.

<!-- BEGIN -->
<p data-id="1">Promethus</p>
<p data-id="2">Orpheus</p>
<p data-id="99">Tantalus</p>
<p data-id="11895">Marcus</p>
<!-- END -->

Delete the line.

// WARNING ! This function can ONLY be called by one process at a time.
function deleteLine($file, $id)
{
    $tmpfile = $file . '.tmp';
    $fp = fopen($file, 'r');
    $fpout = fopen($tmpfile, 'w');

    while ( $line = fgets($fp, 1000) ) {
        // only write lines that are not the id we are searching for
        if ( ! preg_match("/data-id=\"$id\"/", $line) {
            fwrite($fpout, $line . "\n");
        }
    }
    fclose($fp); fclose($fpout);
    rename($tmpfile, $file);
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
0

why do u want to write the online status of users as a static file?

Normally you provide some kind of last_activity timestamp in your users table which can be updated on every request.

Then you can say every user that has the last_activity timestamp within the last 5-10 minutes is treated as online - otherwise he's offline.

If you really want to keep that static file structure you can use the DOMDocument php extension to parse the DOM of the static file in the page and find the entry you want to remove.

lordrvn
  • 46
  • 1
  • The .html file is used as the .html for a list of online users in the website. That's why there's a colour definition in each entry. – python-b5 May 22 '17 at 20:13