• (2 Pages)
  • +
  • 1
  • 2

How Many Users Online Rate Topic: ***** 2 Votes

#1 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1877
  • View blog
  • Posts: 20,284
  • Joined: 17-March 01

Posted 09 December 2004 - 12:40 PM

Level of Difficulty : Beginner
Requirements : PHP

Step1: Creating the database
create a new database called 'users', with 3 fields inside a table. These fields are timestamp, ip and file. Name the table useronline by the way. Now with the form on the PhpMyAdmin page, add the following code:

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);

Step 2: The PHP Script
<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "username"; //your MySQL database username
$db_pass = "password"; //your MySQL database password
$database = "users";
$timeoutseconds = 300;

//this is where PHP gets the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
//$server = localhost probably
//$db_user = your MySQL database username
//$db_pass = //your MySQL database password
mysql_connect($server, $db_user, $db_pass);

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if($user == 1) {
print("$user user online\n");
} else {
print("$user users online\n");
}
?>

I've tried to make that code as simple as possible to understand. The //comment won't affect the script, so you can remove those if you want. The script basically works like this. It receives server information and gets he time. In mySQl it fills in the values of the person online and puts up a message if it failed. When the user leaves mySQL deletes the user and displays a message if it failes.

Is This A Good Question/Topic? 2
  • +

Replies To: How Many Users Online

#2 pete123456  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-February 06

Posted 18 February 2006 - 02:19 PM

Hello,
Thanks for sharing this, I just have a few questions.
First, is the file field just for tracking where the user is going?
Second, I want to incorporate this into all of my pages, how can I make sure that duplicates are avoided?

Thanks very much, I do realise this is an old topic, sorry :sleepy:

Pete
Was This Post Helpful? 0
  • +
  • -

#3 eLliDKraM  Icon User is offline

  • Pepè Le Pewn
  • member icon

Reputation: 6
  • View blog
  • Posts: 6,565
  • Joined: 13-August 05

Posted 18 February 2006 - 06:44 PM

Wouldn't MySQL be a requirement too
Was This Post Helpful? 0
  • +
  • -

#4 Munchen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 06-February 05

Posted 21 February 2006 - 11:58 PM

Yes it would ... Otherwise fine tutorial :)
Was This Post Helpful? 0
  • +
  • -

#5 myharshdesigner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 26-December 07

Posted 26 December 2007 - 09:29 PM

NICE tutorial i like it


View Postskyhawk133, on 9 Dec, 2004 - 12:40 PM, said:

Level of Difficulty : Beginner
Requirements : PHP

Step1: Creating the database
create a new database called 'users', with 3 fields inside a table. These fields are timestamp, ip and file. Name the table useronline by the way. Now with the form on the PhpMyAdmin page, add the following code:

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);

Step 2: The PHP Script
<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "username"; //your MySQL database username
$db_pass = "password"; //your MySQL database password
$database = "users";
$timeoutseconds = 300;

//this is where PHP gets the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
//$server = localhost probably
//$db_user = your MySQL database username
//$db_pass = //your MySQL database password
mysql_connect($server, $db_user, $db_pass);

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if($user == 1) {
print("$user user online\n");
} else {
print("$user users online\n");
}
?>

I've tried to make that code as simple as possible to understand. The //comment won't affect the script, so you can remove those if you want. The script basically works like this. It receives server information and gets he time. In mySQl it fills in the values of the person online and puts up a message if it failed. When the user leaves mySQL deletes the user and displays a message if it failes.

Was This Post Helpful? 0
  • +
  • -

#6 mahendra_22patil  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 17
  • Joined: 15-August 08

Posted 18 August 2008 - 01:23 AM

hi friend,
It is nice code but how to get value of $REMOTE_ADDR and $PHP_SELF :rolleyes: [b][size=7]?
Was This Post Helpful? 0
  • +
  • -

#7 brandon99337  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 167
  • Joined: 14-February 08

Posted 19 August 2008 - 06:47 PM

#REMOTE_ADDR is the IP address of the user, and $PHP_SELF is just the same page that posted it.
Was This Post Helpful? 0
  • +
  • -

#8 RawrAdam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-September 08

Posted 07 September 2008 - 03:37 AM

Thank you for this tutorial its just what I have been looking for! Nice and simple, yet does the job in hand :^:

--
Adam
Was This Post Helpful? 0
  • +
  • -

#9 engale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 3
  • View blog
  • Posts: 594
  • Joined: 30-September 08

Posted 03 October 2008 - 10:51 AM

View Postpete123456, on 18 Feb, 2006 - 02:19 PM, said:

Hello,
Thanks for sharing this, I just have a few questions.
First, is the file field just for tracking where the user is going?
Second, I want to incorporate this into all of my pages, how can I make sure that duplicates are avoided?

Thanks very much, I do realise this is an old topic, sorry :sleepy:

Pete


require_once("filename");
Was This Post Helpful? 0
  • +
  • -

#10 madjam002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-February 08

Posted 05 December 2008 - 01:25 PM

Great tutorial :D
I always wanted to know how to remove the user from the database when they leave :P
Was This Post Helpful? 0
  • +
  • -

#11 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 13 June 2009 - 04:37 AM

really nice tutorial.
Was This Post Helpful? 0
  • +
  • -

#12 Jono20201  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 179
  • Joined: 07-July 09

Posted 07 July 2009 - 11:55 AM

View Postpete123456, on 18 Feb, 2006 - 01:19 PM, said:

Hello,
Thanks for sharing this, I just have a few questions.
First, is the file field just for tracking where the user is going?
Second, I want to incorporate this into all of my pages, how can I make sure that duplicates are avoided?

Thanks very much, I do realise this is an old topic, sorry :sleepy:

Pete


I suppose you could do a function?

function updateUserTime(){
//The tutorial code here
}



then on every page call the file the function is on at the top:
require_once("function.php");

(change function.php to where it is stored.)

then on every page put:
updateUserTime();


Done :D
Was This Post Helpful? 0
  • +
  • -

#13 mulson  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 21-September 09

Posted 03 October 2009 - 03:40 PM

View Postmyharshdesigner, on 26 Dec, 2007 - 08:29 PM, said:

NICE tutorial i like it


View Postskyhawk133, on 9 Dec, 2004 - 12:40 PM, said:

Level of Difficulty : Beginner
Requirements : PHP

Step1: Creating the database
create a new database called 'users', with 3 fields inside a table. These fields are timestamp, ip and file. Name the table useronline by the way. Now with the form on the PhpMyAdmin page, add the following code:

CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);

Step 2: The PHP Script
<?php
//Put your basic server info here
$server = "localhost"; //normally localhost
$db_user = "username"; //your MySQL database username
$db_pass = "password"; //your MySQL database password
$database = "users";
$timeoutseconds = 300;

//this is where PHP gets the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//connect to database
//$server = localhost probably
//$db_user = your MySQL database username
//$db_pass = //your MySQL database password
mysql_connect($server, $db_user, $db_pass);

//insert the values
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}

//grab the results
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}

//number of rows = the number of people online
$user = mysql_num_rows($result);


//spit out the results
mysql_close();
if($user == 1) {
print("$user user online\n");
} else {
print("$user users online\n");
}
?>

I've tried to make that code as simple as possible to understand. The //comment won't affect the script, so you can remove those if you want. The script basically works like this. It receives server information and gets he time. In mySQl it fills in the values of the person online and puts up a message if it failed. When the user leaves mySQL deletes the user and displays a message if it failes.


Hello Sir, i used your code in my site,weldone evrything works well for me...but only that it show only one user online,even if two or more users are online :).... else {
print("$user users online\n");
}
is not functioning :) can you please hint me on that?
Was This Post Helpful? 0
  • +
  • -

#14 Guest_Kamron3*


Reputation:

Posted 24 March 2010 - 03:27 PM

Would it be possible for this script to display everyone who is online? so lets say this

Joe from address 127.0.0.1 is online.
Bob from address 127.1.1.0 is online, too.

The printed MySQL output would be this:
(in alphabetical order)
Bob
Joe
Total Users Online: 2

_k
Was This Post Helpful? 0

#15 Guest_phpflavor*


Reputation:

Posted 18 August 2010 - 11:42 PM

lol simple = is for block of code if want it to come out in a list of files looping try .= and if that doesnt work put a while statment in ur php/mysql code to loop it


View PostJono20201, on 07 July 2009 - 10:55 AM, said:

View Postpete123456, on 18 Feb, 2006 - 01:19 PM, said:

Hello,
Thanks for sharing this, I just have a few questions.
First, is the file field just for tracking where the user is going?
Second, I want to incorporate this into all of my pages, how can I make sure that duplicates are avoided?

Thanks very much, I do realise this is an old topic, sorry :sleepy:

Pete


I suppose you could do a function?

function updateUserTime(){
//The tutorial code here
}



then on every page call the file the function is on at the top:
require_once("function.php");

(change function.php to where it is stored.)

then on every page put:
updateUserTime();


Done :D

Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2