←  PHP Tutorials

Pixel2Life Forum

»

PHP/MySQL Who's Online

HaloprO's Photo HaloprO 29 Jun 2005

http://wam.nrgserver...als/online.html
TutorialNation Tutorials @ http://www.tutorialnation.com/ < Down for maintenance.
Quote

HaloprO's Photo HaloprO 01 Jul 2005

Sorry about that cliff..
MySQL Query
CREATE TABLE `online` (
`username` TEXT NOT NULL ,
`timeout` TEXT NOT NULL
) TYPE = MYISAM;
PHP Code

<?php
session_start
(); #Start the session
$hostname = "hostname"; #MySQL Hostname

$username = "username"; #MySQL Username
$password = "password"; #MySQL Password
$database = "database"; #MySQL Database

$connect = mysql_connect($hostname, $username, $password); #Connect to the mysql host
$select_db = mysql_select_db($database); #Select the database

if (isset($_SESSION['username'])) { #If the user is logged in, good for the, if not, they become an ip address
$username = $_SESSION['username']; #Username is $_SESSION['username'];
} else {

$username = $_SERVER['REMOTE_ADDR']; #Username is IP Address
}
$time = time(); #Current time
$previous = "120"; #Time to check in seconds

$timeout = $time-$previous; #Timeout=time - 2two minutes
$query = "SELECT * FROM online WHERE username=\"$username\" AND timeout > \"$timeout\""; #Have you been here in the past two minutes?
$verify = mysql_query($query); #Execute query

$row_verify = mysql_fetch_assoc($verify); #Check if you have been here in two minutes
if (!isset($row_verify['username'])) { #See if you were found
$query = "INSERT INTO online (username, timeout) VALUES (\"$username\", \"$time\")"; #Put you on the online list

$insert = mysql_query($query); #Execute query
}
$query = "SELECT * FROM online WHERE timeout > \"$timeout\""; #Check and see who is online in the last 2 minutes

$online = mysql_query($query); #Execute query
$row_online = mysql_fetch_assoc($online); #Grab the users
if (isset($row_online['username'])) { #If there is atleast one user online

do { #Do this
echo $row_online['username'].""; #Output username
} while($row_online = mysql_fetch_assoc($online)); #Until all records are displayed

} else {
echo
"There are no members online."; #Inform user that no one is online
}
///////////////////////////
//TutorialNation Tutorial//
///////////////////////////
#If you are wondering what $_SESSION['username'] is all about, you should follow our PHP/MySQL Login Tutorial, It will incorporate well with this tutorial.


?>

Edited by HaloprO, 01 July 2005 - 03:30 PM.
Quote

Ruben K's Photo Ruben K 07 Jul 2005

Also this is not a tutorial, but more of a script.
Quote

Andy's Photo Andy 07 Jul 2005

Yea, nice - but, explain use explanation as your going through the tutorial so people know what you'r on about :)
Quote

Jaymz's Photo Jaymz 07 Jul 2005

Is this tutorial yours?
Quote

HaloprO's Photo HaloprO 23 Jul 2005

Post is a little old, but yeah it's my tutorial
Quote

coderx15's Photo coderx15 27 Dec 2005

Post is a little old, but yeah it's my tutorial


I personally find this tutorial to be, long, On my site (well when i used to work there) i added a script, where, 1 simple mysql query, woud check whos been online in the last 2 minutes, by, on each page it would set last active to time() or w/e. then online.php selects the users with last active set to the current time, and a interval of 10 minutes.. of course it is buggy, sometimes it will say 100 are online when none are, and sometimes the users who arent active anymore stay on the list, yours might not be as buggy though
Quote

Hooch's Photo Hooch 19 May 2006

Is it possible to add this to an existing usersystem?
I have a database set up already. The table name is called "users".
I tried adding the "timeout" field,
then changing the "select from online" to "select from users", but she's a no go.
Any ideas what I'm missing?
ty
**P.S I have a field named "username" too
Edited by Hooch, 19 May 2006 - 10:10 AM.
Quote

Ruben K's Photo Ruben K 20 May 2006

mine is better btw ha ha ha
Quote

Hayden's Photo Hayden 10 Jul 2006

Is it possible to add this to an existing usersystem?
I have a database set up already. The table name is called "users".
I tried adding the "timeout" field,
then changing the "select from online" to "select from users", but she's a no go.
Any ideas what I'm missing?
ty
**P.S I have a field named "username" too


that's my plan.

mine is going to be a closed user system, so you would have to have a login.

so i was just going to replace the username with an integer of the members ID in the database. as well as recording the IP they are logged in with.
Quote

kuguru's Photo kuguru 15 Aug 2006

I'm newb.. and i've some prob here..

msg like this :
Warning: session_start(): Cannot send session cache limiter - headers already sent

what is actually happen and what should i do to fix it?
Quote

Copernicus's Photo Copernicus 04 Sep 2006

Also if you are doing this with members' scripts....add a lastactive field into your users table. Then in a file that is called everywhere (Most likely db connect file) add a snippet that updates lastactive to mktime();

That way you can list whose online member wise by selecting all the users where lastactive is within the last 15 minutes.

Just a little info ;)
Quote