Jump to content

"Is being viewed by now.." question


ruddie

Recommended Posts

Hello, I was wondering how to create the 'This post is being viewed by user1, user2, user3 and ... guests' on a certain location of the forum/website.

 

I was thinking about having all these usernames / guests stored in the mysql database, however this caused an issue for me, since I got no idea if you can/are allowed to place an array in a table.

 

I am hoping someone out here knows what is the easiest/most efficient way of doing this.

 

Thanks in advance.

Link to comment
Share on other sites

For this I would do the following...

- Add a new field on the users DB called current_location

- When a person loads a page, update the current_location with the page name

- Then you can select from the database all of the people currently viewing the page and display the count

 

This is probably the easiest method, but someone else may have something better.

 

Regards, PaulRyan

Link to comment
Share on other sites

For this I would do the following...

- Add a new field on the users DB called current_location

- When a person loads a page, update the current_location with the page name

- Then you can select from the database all of the people currently viewing the page and display the count

 

This is probably the easiest method, but someone else may have something better.

 

Regards, PaulRyan

 

I was in fact thinking of that, but I was stuck on the point where people would just exit the browser, what would happen then? Is there a function that somehow gets called when they do this? (I.e. Server loses connection ..)

Link to comment
Share on other sites

Paul is correct and you should look to use his method. I'd only change it if you wanted a 'history' type feature, where you can show the last 5 - for example - places the user has visited.

 

The only thing I would add is a 'last_seen' field. This should store the last time the user visited any page on the website and should be updated when the current_location field is. This is so you're not stuck with a potentially huge list containing users who navigated away from your website when visiting a certain page.

 

So to recap:

- Use Paul's method unless you're looking for a "history" feature.

- Add a last_seen field to the DB.

- Update the field when current_location is updated.

- Decide the 'timeout' time so you're not left with users who aren't online any more.

 

Hope this helps.

 

Michael.

Link to comment
Share on other sites

Damn MikeDean, knew I forgot something :)

 

You'd have to update the last_seen field with a Unix Timestamp for simpleness.

Then only select people from the DB who have been online in the last 10 minutes or so?

 

Something like...

<?PHP
  $lastSeen = time()-600; // Upto and including 10 minutes ago
  $myQuery = "SELECT username FROM users WHERE current_location='$thisPage' AND last_seen>$lastSeen AND username!='$loggedUser' ORDER BY last_seen";

  if($doQuery = mysql_query($myQuery)) {
    if(mysql_num_rows($doQuery)) {

      $pageViewers = $loggedUser;

      while($user = mysql_fetch_assoc($doQuery)) {
        $pageViewers .= ', '.$user['username'];
      }

      echo $pageViewers;

    } else {
      echo $loggedUser;
    }
  } else {
    echo 'This query failed: '.$myQuery
  }
?>

 

$loggedUser - The user that is currently logged in, if not logged in set this variable blank.

 

Something like the above would work :)

 

Regards, PaulRyan.

Link to comment
Share on other sites

In fact.. I have been thinking a bit again - And I got no idea how to measure 'Guests'. I guess these will work the same way, but I was thinking about somehow using these people there IP? Perhaps I'll make a 'guest' table for these. I would like to hear your insights on this.

Link to comment
Share on other sites

Ah, I think if you're looking to monitor guests too then I'd move this entirely into a new table (so even for users).

 

You can still keep the general idea of what Paul and I (well, mostly Paul) have been discussing. Here's a few things I'd do for this type of feature.

 

- Create an online table with the following fields

-- id

-- session_id.

-- user_id (default this to 0 which will represent a guest, otherwise, it'll store the logged in users ID).

-- IP

-- current_location

-- last_seen

 

- Still update both current_location and last_seen but also look to run a query that deletes any users that are now logged off, i.e. been offline for more than 10 minutes.

$lastSeen = time()-600; // Upto and including 10 minutes ago
$myQuery = "DELETE FROM online WHERE last_seen<$lastSeen";

 

I haven't got a lot of time to go into more detail but that should give you a general idea of how this could work.

 

Of course, there could be a better, more efficient way of doing this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.