Link to home
Start Free TrialLog in
Avatar of SniperCode Sheva
SniperCode Sheva

asked on

How to check if a session is disconnected in php

Hello, I am doing a chat, and to get the names of users for my chat I am using session, so how I can get the users connected and check if they are disconnected or not.
Any idea ?
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Do you mean Connected by they have an open tab of your website in their browser or something else?
Avatar of SniperCode Sheva
SniperCode Sheva

ASKER

I mean the session is started, in my website I have a connection form and I am using session for the connection
For this, you can use a database or the file system.
My idea is  in my database I have a field "connected" which can have 1 or 0 so  if the user is connected the field become 1 and when the user is disconnected the field become 0 but my problem is to check if the user is connected or disconnected in order to update the field.
How you are tracking user login state?
you probably should update a field with the last date the user performed an action. disconnected users are the ones that have not been active for a while. you can match the duration with the php session timeout for consistency.

if you need something precise, you can include a small script on your site that will periodically call a given url that performs the update whenever the user has an open page on your site. that will let you make the period short ( such as 5 seconds ) and will work as long as the page is open even if the user is drinking coffee next door
I have only a session_start() nothing else.
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First make a database table named users.I suggest you to have 3 columns id-session-time.

Make the a query that checks and count if the unique the uset session exist in the table.If not the INSERT INTO table .If the session exist  then update the time field into the table.

Then with a $.ajax function you can retrieve the time data from table correspondily to the user.

At the end with a javascript code check if the time data that you retrieved is less than eg. 60sec from now() to check if the user is online or (probably) offline.