Jump to content


Photo
- - - - -

PHP/MySQL Who's Online


  • Please log in to reply
12 replies to this topic

#1 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 29 June 2005 - 01:44 AM

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

#2 Ruben K

Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 29 June 2005 - 06:21 AM

http://www.pixel2lif...?showtopic=3895
:P

#3 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 01 July 2005 - 03:25 PM

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.


#4 Ruben K

Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 07 July 2005 - 05:50 AM

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

#5 Andy

Andy

    Graphical Idol Judge

  • Members
  • PipPipPip
  • 594 posts
  • Gender:Male
  • Location:Yorkshire, UK

Posted 07 July 2005 - 07:09 PM

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

#6 Jaymz

Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 07 July 2005 - 07:36 PM

Is this tutorial yours?

#7 HaloprO

HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 23 July 2005 - 04:44 AM

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

#8 coderx15

coderx15

    Young Padawan

  • Members
  • Pip
  • 3 posts

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 Hooch

Hooch

    Young Padawan

  • Members
  • Pip
  • 158 posts
  • Location:Brockville, Ontario

Posted 19 May 2006 - 10:09 AM

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.


#10 Ruben K

Ruben K

    Cliff

  • Twodded Staff
  • PipPip
  • 438 posts

Posted 20 May 2006 - 08:46 AM

mine is better btw ha ha ha

#11 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

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 kuguru

kuguru

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 15 August 2006 - 09:05 AM

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?

#13 Copernicus

Copernicus

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 04 September 2006 - 10:19 PM

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 ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users