PHP/MySQL Who's Online
#1
Posted 29 June 2005 - 01:44 AM
TutorialNation Tutorials @ http://www.tutorialnation.com/ < Down for maintenance.
#3
Posted 01 July 2005 - 03:25 PM
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.
#4
Posted 07 July 2005 - 05:50 AM
#5
Posted 07 July 2005 - 07:09 PM
#6
Posted 07 July 2005 - 07:36 PM
#7
Posted 23 July 2005 - 04:44 AM
#8
Posted 27 December 2005 - 12:21 PM
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
#9
Posted 19 May 2006 - 10:09 AM
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.
#10
Posted 20 May 2006 - 08:46 AM
#11
Posted 10 July 2006 - 06:16 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.
#12
Posted 15 August 2006 - 09:05 AM
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?
#13
Posted 04 September 2006 - 10:19 PM
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
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users