Answer the question
In order to leave comments, you need to log in
How to find out which user is on the page?
I am making a chat, and I want to display those users who are currently on the chat page, how to implement this?
Answer the question
In order to leave comments, you need to log in
1) From the server side in php, you can use the "last visit" to fix that the user is on the site, hoping that a second after refreshing the page, and therefore this field, he did not close the page and did not forget about your chat
2) From the client side, as something like this: if the chat is the whole page, then on js / jquery to the closing event of the tab / page / browser, attach an exit from the online. But if the chat is some area on the page, for example, like a chat tab in the corner of the VK site, then in addition to the tab closing event, leaving the online must be added to the closing event of this page area (if we are talking about being inside the chat) ... T. e. php online opens, and events on js close.
Thus, you can achieve maximum objectivity of this mark that the user is actually online
Try using a database
$my_id = when a person registers you give him an ID
For example: $my_id = $_SESSION['id'];
or
$my_id = $_COOKIE['id'];
you make a table in sql with the name online
in online you make tables ip, user, ip
if (isset($my_id)) {$user_online = $my_name;}
else {$user_online="guest";}
if ($user_online == 'guest') {$Online = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT `ip` FROM `online` WHERE `ip` = '$_SERVER[REMOTE_ADDR]'"));}
else {$Online = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT `user` FROM `online` WHERE `user` = '$user_online'"));}
if ($Online['ip']) {mysqli_query($mysqli, "UPDATE `online` SET `time` = NOW() WHERE `ip` = '$_SERVER[REMOTE_ADDR]'");}
else if ($Online['user'] && $Online['user'] != 'guest') {mysqli_query($mysqli, "UPDATE `online` SET `time` = NOW() WHERE `user` = '$user_online'");}
else {if($user_online)mysqli_query($mysqli, "INSERT INTO `online` SET `ip` = '$_SERVER[REMOTE_ADDR]', `user` = '$user_online', `time` = NOW()");}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question