On Fri, Dec 26, 2008 at 20:24, Stephen Alistoun
<stephenalist...@gmail.com> wrote:
>
> Hi all,
>
> What is the best way to pick up all the members online on a website?

    Check the archives and STFW for examples, but the general gist is
of it is to use $_SESSION tracking with activity checking.  I'm just
typing this in quickly as I go, so it's untested (read: don't copy and
paste for production), but here's a simple example (you should be able
to fill in the blanks easily):

<?php
function m($str) { // Just an alias to shorten typing here.
    return mysql_real_escape_string($str);
}

session_start();

// You can add their username if they're logged in, or display
// the count of unauthenticated users active on the site as the
// collective "# Guests Online" group.
mysql_query("UPDATE `users_online` SET `ip` =
'".m($_SERVER['REMOTE_ADDR'])."', `last_active` = '".m(time())."`
WHERE `sess_id` = '".m(session_id())."'";

if (mysql_affected_rows() == 0) {
        mysql_query("INSERT INTO users_online(sess_id,ip,last_active)
VALUES('".m(session_id())."','".m($_SERVER['REMOTE_ADDR'])."','".m(time())."')";
}

// Et cetera....

// Now, to display users online:
$sql = "SELECT username FROM users_online WHERE last_active -
".m(time())." < 300";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
    echo $row['username']."<br />\n";
}

// And guests:
$sql = "SELECT COUNT(*) AS num_guests FROM users_online WHERE
username='' AND last_active - ".m(time())." < 300";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo "There are ".$row['num_guests']." guests online.<br />\n";

?>

-- 
</Daniel P. Brown>
daniel.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to