Jump to content

whos online


Danny620

Recommended Posts

Add a new field to your users table like:

 

last_click_at DATETIME

 

Then using the below query you can find which users are currently online (clicked in the last 5 minutes)

 

SELECT * FROM users WHERE last_click_at BETWEEN now() - 300 AND now()

 

Ofcourse this implies that you update this field on every click.

Link to comment
Share on other sites

Hi

 

Doing this requires every page to update the user who has visited that page (probably in a common routine included at the top of each script).

 

Eg, this routine, that also redirects people out of the system if they haven't done anything for X minutes (this is for Access SQL, but much the same for MySQL):-

 

<?php
$sSql = "Update AdminLogin Set LastRefresh = now() where Username = '".$_SESSION['Username']."' AND SessionId = '".session_id()."' and datediff('n',lastrefresh,now()) <= 60";
$resultSec = odbc_exec($Conn,$sSql);
if ($resultSec)
{
if (odbc_num_rows($resultSec) < 1)
{
	header('Location:Logout.php');
	exit();
}
}
?>

 

All the best

 

Keith

Link to comment
Share on other sites

yes i think i understand but can u explain abit more like maybe and update page that updates that user

 

Assuming a table structure like:

 

CREATE TABLE users (
  users_id INTEGER NOT NULL AUTO_INCREMENT,
  users_last_click_at DATETIME,
  PRIMARY KEY (users_id)
);

 

Your update code would be:

 

if (isset($_SESSION['users_id'])) {
    $id = abs((int) $_SESSION['users_id']);//set at login
    if (!$id) // do something: $id was 0
    
    $query = 'UPDATE users SET users_last_click_at = now() WHERE users_id = %d';
    $fquery = sprintf($query, $id);
    mysql_query($fquery/*, $db*/);
}

 

Your members online:

 

$query = 'SELECT count(*) users_online FROM users WHERE users_last_click_at BETWEEN now() - 300 AND now()';
if ($result = mysql_query($query/*, $db*/)) {
    list($users_online) = mysql_fetch_row($result);
}

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.