[help] Who's online script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BigRed
Forum Newbie
Posts: 6
Joined: Thu Dec 26, 2002 11:47 am

[help] Who's online script

Post by BigRed »

Hi, how are ya?

I'm writing a message board right now and I need help writing a script that shows how many users and guests (and displays the registered usernames) have been online in the past 5 minutes.

Can someone tell me how and what database I need to create with what tables and such? I use sessions on my forum, so when I user logs in, they are forwarded to a page that sets the session. I thought maybe that would be a good place where code can be put in to update a table with their login name and ip and such but since I'm very new to PHP I have no idea where to begin on this one.

Thanks in advance :)
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Here is a little script I include right after starting a session and including the config file.

Code: Select all

<?php
  	 $thetime=date("h:i:s A T");
	 $one=time();
	 $two=($one - $cutout);
$mysql = mysql_select_db( "$DBname" );
$sql = ("DELETE FROM users_online WHERE timestamp < '".$two."'");
$result=mysql_query($sql);
  $lastip=($REMOTE_ADDR);
     $hostname=(NA); $timestamp=time();
  if (empty($_SESSION['valid_user'])) { $ol_user=(Guest); }
  else { $ol_user=($valid_user); }  
  // if the user has just tried to log in
  $db_conn = mysql_connect("localhost", "$DBuser", "$DBpass");
  mysql_select_db("league");
  $query = "select * from users_online where lastip='$lastip'";
  $result007 = mysql_query($query, $db_conn);
  if (mysql_num_rows($result007) >0 )
  { // then we update
  $mysql = mysql_select_db( "$DBname" );
  $sql=("UPDATE users_online SET ol_user='$ol_user', hostname='$hostname', timestamp='$timestamp', page='$page', thetime='$thetime' WHERE lastip='$lastip'");
  $result2=mysql_query($sql);
  }
  else 
  {
// insert users online
$mysql = mysql_select_db( "$DBname" );
$sql="insert into users_online values('id','$ol_user','$lastip','$hostname','$timestamp','$page','$thetime')";
$result2=mysql_query($sql);
}
?>
Now $cutout is in my config file so I can set the time there.

if you use this or something like that you will have your database entries. then you need to build your whos online viewing. I use 2. One to show the total people online ( This is in a side box) then a web page to show where they are.
User avatar
BigRed
Forum Newbie
Posts: 6
Joined: Thu Dec 26, 2002 11:47 am

Post by BigRed »

Thanks.
How many tables should I create in the database and what fields should they have? I'm new to PHP. I need a little more info than you gave me. Sorry. :?

Edited: Ok, I sort of got it now. How do I make it display users that have been on in the last 5 minutes ONLY?
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Sorry. If you look at the insert into the database you see how many. In the case that i gave you anyway. I have 1 there with 7 fields. The hostname is not used yet but eventually will be.

Code: Select all

CREATE TABLE users_online (
  id int(10) NOT NULL auto_increment,
  ol_user varchar(40) NOT NULL default 'Guest',
  lastip varchar(30) NOT NULL default '',
  hostname varchar(30) NOT NULL default '',
  timestamp varchar(30) NOT NULL default '',
  page varchar(100) NOT NULL default '',
  thetime varchar(50) NOT NULL default '',
  PRIMARY KEY  (id)
) TYPE=MyISAM;
That one table is all you need. All entries will be purged that are over the allowed time of inactivity when a page is loaded.

email me if you have more questions.
jerry@coastgames.com
Post Reply