0

i have a website where you can login and logout. If you login it will do this code in PHP:

$status = "Active now"; 
$sql2 = mysqli_query($conn, "UPDATE users SET status = '{$status}' WHERE unique_id = {$row['unique_id']}");

And if you logout it will do this:

$status = "Offline now";
$sql = mysqli_query($conn, "UPDATE users SET status = '{$status}' WHERE unique_id {$_GET['logout_id']}");

But the problem is that when a user just closes the tab or the browser without pressing on the "Logout" button it will still say "Online now" in the database. How do i change this? (Please dont answer with "window.unload" or something like that. I heard that you can do it with something called like "sendBeacon()" or "Ajax". But how do i use sendbeacon function in my case? I am really new to this. Thousands Thanks!!

Temal
  • 11
  • 2
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 27 '21 at 12:10
  • Try socker io (WEB SOCKET). Then server will receive disconnected event. Then you can update the status in background – Karthik Sridharan Aug 04 '22 at 07:22

0 Answers0