For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > January 2005 > counting CURRENT site visitors









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author counting CURRENT site visitors
toedipper

2005-01-22, 8:56 pm

Hello,

I am developing a site using php and mysql and I would like to be able to
let visitors know how many other visitors are currently browsing the site
i.e. 'There are currently x amount of visitors browsing this site'

You can see this in action in the following sites but I am not sure what
technology they use www.recruitni.com (about 1/3 down the page) and also
www.scancom.co.uk on the left hand side.

Is this possible using PHP and can anyone point me in the direction of how
to achieve it?

Thanks.

td

http://www.pocketpcheaven.blogspot.com/



Michael Fesser

2005-01-22, 8:56 pm

.oO(toedipper)

>I am developing a site using php and mysql and I would like to be able to
>let visitors know how many other visitors are currently browsing the site
>i.e. 'There are currently x amount of visitors browsing this site'


If you want the value to be accurate -- impossible.

>You can see this in action in the following sites but I am not sure what
>technology they use www.recruitni.com (about 1/3 down the page) and also
>www.scancom.co.uk on the left hand side.


HTTP is a stateless protocol, there's no way to determine how many
visitors are currently "online", because there simply is no way to
identify unique users except for an explicit login. It's all based on
estimations (for example the amount of requests from different IPs in
the last 10 minutes) and as useless as a hitcounter for a visitor. The
number of currently browsing users or page hits says absolutely nothing
about a site's quality.

Micha
Ghizmo

2005-01-23, 3:56 am

On Sat, 22 Jan 2005 22:50:05 -0000, "toedipper"
<send_rubbish_here734@hotmail.com> wrote:

>I am developing a site using php and mysql and I would like to be able to
>let visitors know how many other visitors are currently browsing the site
>i.e. 'There are currently x amount of visitors browsing this site'


For my website I use this method...

$timestamp=time();

// 10 minutes of timeout
$timeout=$timestamp-6000;

mysql_query("INSERT INTO online (time,ip) VALUES
('$timestamp','$REMOTE_ADDR')");

// purge all old users
mysql_query("DELETE FROM online WHERE ora < $timeout");

// delete my own ip adress from statistic
mysql_query("DELETE FROM online WHERE ip = 'xxx.xxx.xxxx.xxxx'");

$result=mysql_query("SELECT DISTINCT ip FROM online");
$user=mysql_num_rows($result);

echo $user;




RavenSlay3r

2005-01-23, 8:57 pm

Nearly exactly what I was going to say Ghizmo.

The simpliest way is to write a script that inserts (or updates) the IP
and Time/date in a DB table. Then count the # of unique IP's and puge
the old one's. Most sites look at a 10 min. interval for this purpose;
given the nature of HTTP.

Call [the file] "userCount.php" or somthing and include it at the top
of your header so it's called on every page hit.

------
I'm going to modify Ghizmo's code to show what I was thinking. Haven't
tested this but I'm 99% sure it's right (and more efficent).

REPLACE should INSERT the value if the record doesn't exist and DELETE
& INSERT if it does, keeping the IP coloumn unique.

(if you try this let me know how it works.)

------
// ** Begin userCount.php **//

$timestamp=time();

// 10 minutes of timeout
$timeout=$timestamp-6000;

// REPLACE instead of INSERT
mysql_query("REPLACE INTO online (time,ip) VALUES
('$timestamp','$REMOTE_ADDR')
WHERE ip = '$REMOTE_ADDR')";

// purge all old users
mysql_query("DELETE FROM online WHERE time < $timeout");

// delete my own ip adress from statistic
mysql_query("DELETE FROM online WHERE ip = 'xxx.xxx.xxxx.xxxx'");

$result=mysql_query("SELECT ip FROM online");
$user=mysql_num_rows($result);
echo $user;

// ** End userCount.php **//

Ghizmo

2005-01-23, 8:57 pm

On 23 Jan 2005 01:26:40 -0800, "RavenSlay3r" <ravenslay3r@gmail.com>
wrote:

>Nearly exactly what I was going to say Ghizmo.
>REPLACE should INSERT the value if the record doesn't exist and DELETE
>& INSERT if it does, keeping the IP coloumn unique.


Thanks Raven for completing my script. The reason of the Insert in my
script is because I it to keep track of all the movments in my
website. My complete Query is

mysql_query("INSERT INTO online (time,ip,where) VALUES
('$timestamp','$REMOTE_ADDR','$PHP_SELF'
)");

and keep records for 24hours instead of 10 minutes.
So I can see exactly where the user moves.

Bye bye


Salutoni,
Ghizmo
---------
-----------
------------
Webmaster di
www.agenziaoksana.com
---------------------

R. Rajesh Jeba Anbiah

2005-01-26, 3:57 pm

toedipper wrote:
> Hello,
>
> I am developing a site using php and mysql and I would like to be

able to
> let visitors know how many other visitors are currently browsing the

site
> i.e. 'There are currently x amount of visitors browsing this site'
>
> You can see this in action in the following sites but I am not sure

what
> technology they use www.recruitni.com (about 1/3 down the page) and

also
> www.scancom.co.uk on the left hand side.
>
> Is this possible using PHP and can anyone point me in the direction

of how
> to achieve it?


This is much easier if you use custom DB based session handler
<http://in.php.net/session_set_save_handler>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com