Help - Search - Members - Calendar
Full Version: PHP/MySQL Who's Online
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
HaloprO
http://wam.nrgservers.net/tutorials/online.html
TutorialNation Tutorials @ http://www.tutorialnation.com/ < Down for maintenance.
HaloprO
Sorry about that cliff..
MySQL Query
CODE
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.


?>
Ruben K
Also this is not a tutorial, but more of a script.
Andy
Yea, nice - but, explain use explanation as your going through the tutorial so people know what you'r on about smile.gif
Jaymz
Is this tutorial yours?
HaloprO
Post is a little old, but yeah it's my tutorial
coderx15
QUOTE(HaloprO @ Jul 23 2005, 05:44 AM) *
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
Hooch
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
Ruben K
mine is better btw ha ha ha
Hayden
QUOTE(Hooch @ May 19 2006, 03:09 PM) *
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.
kuguru
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?
Copernicus
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 smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.