George
George

Reputation: 3953

How to monitor users in forum page

Is there a way to monitor a users activities in a particular web page? What I want to do is to see if a particular user is currently active in a forum, and if not active, the user should be displayed as absent from forum

 $sql   =   "SELECT * FROM forumlog WHERE user_id = ".$_SESSION['user_id'];
 $result    =   mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($result) == 0){
        $sql   = "INSERT INTO forumlog (course_id, user_id, log_time)
                VALUES(
                ".$_GET['course_id'].",
                ".$_SESSION['user_id'].",
                ".time().")";
       $result    =   mysql_query($sql) or die(mysql_error());
}

The above code is what I use to log the user into the forum, this is because the forum is just a page on my site, and until the user clicks on the forum link he would not be logged in, but then I don't how to go about logging out the user from the forum page or checking to see if the user is active in that forum

Upvotes: 2

Views: 310

Answers (4)

Salvatorelab
Salvatorelab

Reputation: 11873

The traditional way of doing this has already been explained (updating the "last_activity" field every time the user loads some content).

I'm going to point a totally different way of doing this: websockets.

As you know http works in a request-response way. Connection is created by the client, and closed after a response has been sent (well, not always, depending on keepalive). So your problem ("how can I know if the user is still there?") just can't be solved unless you accept this: "if the user is not requesting more content, consider he's not online anymore".

Well, websockets have an advantage here, as the connection is not closed until the client or the server decide it's time to disconnect. So you can open a connection when the user enters the forum, and leave it there until the client leaves the page or closes the browser and thus the connection is closed.

The bad news: you have to write a server that listens for those connections. For example using Node.js. It's not difficult, but you need root access to the server, and then write a few lines of code.

The traditional way is easier, it's just an sql query, but it's good to know about alternatives.

edit

Quick example

  1. Install Node.js: http://nodejs.org/
  2. Install socket.io (it's a node.js module): http://socket.io/
  3. Write a simple server like this:
var socketsConnected = 0;
var io = require('socket.io').listen(8080);
io.sockets.on('connection', function (socket) {
    socketsConnected++;
    // send him some information
    socket.emit('totalSocketsOnline', { counter: socketsConnected });
    // let the others know that the counter has changed!
    socket.broadcast.emit('totalSocketsOnline', { counter: socketsConnected });

    // socket events
    socket.on('disconnect', function () {
        console.log("Socket disconnected");
        socketsConnected--;
        // again, notify all clients!
        socket.broadcast.emit('totalSocketsOnline', { counter: socketsConnected });
    });
});

 4- Write a simple client like this:

<script src="http://yourdomain.com:8080/socket.io/socket.io.js"></script>
<script>
    var socket = io.connect('http://yourdomain.com:8080');
    socket.on('connect', function (data) {
        // connected, we are so happy...
    });
    socket.on('totalSocketsOnline', function (data) {
        // wohohoo lets show how many sockets we have online now
        alert("We have "+data['counter']+" sockets online now!");
    });
</script>

I've just coded that, so it will probably contain errors and won't work, but it's a place to start.

Upvotes: 2

user2503040
user2503040

Reputation:

If you could go through a little stress you could make an open connection to the server using polling, and return an array position of the mouse for a span of 30 seconds, if the position remains the same with previous 30 seconds then you would conclude the user is not currently active. and could make a script to log the user out..... well, am just saying....

Upvotes: 1

Mr. Alien
Mr. Alien

Reputation: 157374

You can do it like this, have a last_seen column in your DB, probably users column

Now Log the login activity of the user, when user switch between forum pages, keep updating the last_seen column, now to detect whether the user is online or offline you need to subtract the last_seen time from current time

if(strtotime($current_time) - strtotime($last_seen) < 60) { //60 Seconds /1 minute
   echo 'User Is Online';
} else {
   echo 'User Is Offline';
}
/* This will mark users online if they 
   were active before a minute, certainly you can extend it to 2-3 minutes  
   i.e 120/180 sec */

Upvotes: 2

Robert
Robert

Reputation: 20286

When user enters page you can put the time in database. Everytime he refreshes page you should update this time.

To check which users are active you must select from database users that "time" is less than 5 minutes.

To check what users are absent you need to check users than time is greater than 5 minutes.

5 minutes of course can be changed to value you want.

If you want to be more particular you can name actions on your website and put it to database in structure like:

id, user_id, datetime, action, params

and put records

1, NOW(), 4,  "viewforum", "forum_id=4,post_id=6"

and then you can select activites in last 5 minutes and check by user_id who is online.

Upvotes: 3

Related Questions