Re: Online Members

From: Date: Sat, 27 Dec 2008 17:07:01 +0000
Subject: Re: Online Members
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-285096@lists.php.net to get a copy of this message
On Fri, Dec 26, 2008 at 20:24, Stephen Alistoun <stephenalistoun@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.brown@parasane.net || danbrown@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Unadvertised dedicated server deals, too low to print - email me to find out!

« previous php.general (#285096) next »