School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,978 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,908 people online right now. Registration is fast and FREE... Join Now!




How Many Users Online

 
Reply to this topicStart new topic

> How Many Users Online

Rating  5
skyhawk133
Group Icon



post 9 Dec, 2004 - 11:40 AM
Post #1


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:

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
CODE
<?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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

pete123456
*



post 18 Feb, 2006 - 01:19 PM
Post #2
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.gif

Pete
Go to the top of the page
+Quote Post

eLliDKraM
Group Icon



post 18 Feb, 2006 - 05:44 PM
Post #3
Wouldn't MySQL be a requirement too
Go to the top of the page
+Quote Post

Munchen
*



post 21 Feb, 2006 - 10:58 PM
Post #4
Yes it would ... Otherwise fine tutorial smile.gif
Go to the top of the page
+Quote Post

myharshdesigner
*



post 26 Dec, 2007 - 08:29 PM
Post #5
NICE tutorial i like it


QUOTE(skyhawk133 @ 9 Dec, 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:

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
CODE
<?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.

Go to the top of the page
+Quote Post

mahendra_22patil
*



post 18 Aug, 2008 - 12:23 AM
Post #6
hi friend,
It is nice code but how to get value of $REMOTE_ADDR and $PHP_SELF rolleyes.gif [b][size=7]?
Go to the top of the page
+Quote Post

brandon99337
**



post 19 Aug, 2008 - 05:47 PM
Post #7
#REMOTE_ADDR is the IP address of the user, and $PHP_SELF is just the same page that posted it.
Go to the top of the page
+Quote Post

RawrAdam
*



post 7 Sep, 2008 - 02:37 AM
Post #8
Thank you for this tutorial its just what I have been looking for! Nice and simple, yet does the job in hand icon_up.gif

--
Adam
Go to the top of the page
+Quote Post

engale
Group Icon



post 3 Oct, 2008 - 09:51 AM
Post #9
QUOTE(pete123456 @ 18 Feb, 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.gif

Pete


require_once("filename");
Go to the top of the page
+Quote Post

madjam002
*



post 5 Dec, 2008 - 12:25 PM
Post #10
Great tutorial biggrin.gif
I always wanted to know how to remove the user from the database when they leave tongue.gif
Go to the top of the page
+Quote Post

noorahmad
Group Icon



post 13 Jun, 2009 - 03:37 AM
Post #11
really nice tutorial.
Go to the top of the page
+Quote Post

Jono20201
**



post 7 Jul, 2009 - 10:55 AM
Post #12
QUOTE(pete123456 @ 18 Feb, 2006 - 01: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.gif

Pete


I suppose you could do a function?

CODE

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


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

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

then on every page put:
CODE
updateUserTime();


Done biggrin.gif


Go to the top of the page
+Quote Post

mulson
*



post 3 Oct, 2009 - 02:40 PM
Post #13
QUOTE(myharshdesigner @ 26 Dec, 2007 - 08:29 PM) *

NICE tutorial i like it


QUOTE(skyhawk133 @ 9 Dec, 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:

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
CODE
<?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 smile.gif.... else {
print("$user users online\n");
}
is not functioning smile.gif can you please hint me on that?
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:15AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month