Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check in real time if a user is logged in?

I am building a simple support chat for my website using Ajax. I would like to check if the user that I am currently chatting with left the browser.

At the moment I have build in that feature by setting interval function at customer side that creates the file with name: userId.txt

In the admin area I have created an interval function that checks if userId.txt exists. If it exists, it deletes it. If the file is not recreated by the custom interval function - next time the admin function will find out that file is not there it mark customer with this userId as inactive.

Abstract representation:

customer -> interval Ajax function -> php [if no file - create a new file]
admin -> interval Ajax function -> php [if file exists - delete the file] -> return state to Ajax function and do something

I was wondering if there is any better way to implement this feature that you can think of?

like image 807
DevWL Avatar asked Sep 16 '15 03:09

DevWL


People also ask

Which of the following can be used to check if a user is logged in or not?

The function, is_authenticated() , then checks to see if the user is authenticated (logged in). If so, we print out, "Logged in". If not, we print out, "Not logged in".

How do I track user logs in Active Directory?

To view the events, open Event Viewer and navigate to Windows Logs > Security. Here you'll find details of all events that you've enabled auditing for. You can define the size of the security log here, as well as choose to overwrite older events so that recent events are recorded when the log is full.

How do I check my login activity on a server?

Step 1 – Go to Start ➔ Type “Event Viewer” and click enter to open the “Event Viewer” window. Step 2 – In the left navigation pane of “Event Viewer”, open “Security” logs in “Windows Logs”. Step 3 – You will have to look for the following event IDs for the purposes mentioned herein below.


2 Answers

My solution is to use the jquery ready and beforeunload methods to trigger an ajax post request that will notify when the user arrives and leaves. This solution is "light" because it only logs twice per user.

support.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

//log user that just arrived - Page loaded
$(document).ready(function() {
         $.ajax({
        type: 'POST',
        url: 'log.php',
        async:false,
        data: {userlog:"userid arrived"}
    });
});

//log user that is about to leave - window/tab will be closed.
$(window).bind('beforeunload', function(){
    $.ajax({
        type: 'POST',
        url: 'log.php',
        async:false,
        data: {userlog:"userid left"}
    });
});
</script>
</head>
<body>

<h2>Your support html code...</h2>

</body>
</html>

log.php

<?php

//code this script in a way that you get notified in real time
//in this case, I just log to a txt file

$userLog = $_POST['userlog'];
file_put_contents("userlog.txt", $userLog."\n", FILE_APPEND );
//userid arrived
//userid left

Notes:

1 - Tested on Chrome, FF and Opera. I don't have a mac so I couldn't test it on Safari but it should work too.
2 - I've tried the unload method but it wasn't as reliable as beforeunload.
3 - Setting async to false on the ajax request means that the statement you are calling has to complete before the next statement, this ensures that you'll get notified before the window/tab is closed.

like image 199
Pedro Lobito Avatar answered Oct 20 '22 04:10

Pedro Lobito


@Gonzalon makes a good point but using a normal DB table or the filesystem for constantly updating user movement would be exhaustive to most hard disks. This would be a good reason for using shared memory functions in PHP.

like image 36
Devon Avatar answered Oct 20 '22 04:10

Devon