Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping track of logged in users in PHP

Tags:

php

session

I'm trying to write a simple web chat app with PHP and AJAX.

I need to be aware of all the open sessions so that I can display a list of online users that are available to talk to. I also need to be aware of log outs because I use "both the sending and the receiving party are offline" as a condition for considering a chat session terminated and deleting the messages.

I'm keeping track of logged in users in a database: adding an entry on log-in and removing it on log-out works fine, but it's not comprehensive, as there are two other ways a user can become logged out:

  • server side session expires after inactivity.
  • client side cookie gets destroyed on browser close. Seems like a bad idea to use some sort of onclose triggered AJAX (what if the browser crashes or something?).

Simplest solution seems to be keeping a timestamp of last activity. I see some problems with this though:

  • AFAIK server-side expiry is chance based, so it wouldn't be accurate (and if I get the expiry time 3 minutes wrong, that's 3 minutes where some guy could be talking to an offline user wondering why no one is answering)
  • I'd have to constantly be querying the database to check every logged in users' last activity time compared to the current time. I don't see when / where I'd do this efficiently. It seems stupid to do this every single time the list of online users is needed.

Any help appreciated. I'm coding this myself because I'm not aware of any web chat frameworks that can integrate with my existing user database, do correct me if I'm wrong.

like image 522
Jonathan Avatar asked Aug 24 '11 12:08

Jonathan


People also ask

How to display logged in user information in php?

The register. php page asks for the desired username, email, and password of the user, and then sends the entered data into the database, once the submit button is clicked. After this, the user is redirected to the index. php page where a welcome message and the username of the logged-in user is displayed.

How to display username after login in php using session?

php session_start(); if($_SESSION['logged']==true){ echo $_SESSION["username"]; echo '<a href="logout. php"><span>Logout</span></a></li>'; } elseif($_SESSION['logged']==false) echo '<a href="registerform. html"><span>Login/Register</span></a></li>'; ?>

How to print welcome username in php?

You should change this to: $_SESSION['username'] = $myusername; $_SESSION['password'] = $mypassword; Because $myusername and $mypassword refer to the username and password sent to the script from the form data. Thank you so much, can not believe i missed that -_-


1 Answers

I don't think you're going to be able to do much to mitigate the constant querying to determine if users have logged off by closing the browser, internet connection issues, etc., but you could perhaps have each client make an AJAX request to the server every 5 seconds to update the last activity time, and have your application on the server consider the user "logged off" if they have missed 3-4 consecutive requests (ie, their last activity time is > 20 seconds).

On the client side, you could then check the last activity time every time your client sends a message to another user, and respond that they'd logged off if that had happened. If they attemped to open a chat with another user, you could also do an immediate call to check their status. Then you could perhaps check the status of all users in the user list every 30 seconds. That way your client gets pretty quick feedback if the person (s)he is chatting with drops offline unexpectedly.

like image 105
Jeremy Clifton Avatar answered Oct 25 '22 15:10

Jeremy Clifton