Forum Moderators: coopster

Message Too Old, No Replies

Number of users/visitors current browsing the site

         

kenchix1

4:42 am on Jun 18, 2005 (gmt 0)

10+ Year Member



is there a way to create a php script where I will know the exact number of users/visitors currently browsing the site?

thanks.

kenchix1

4:54 am on Jun 18, 2005 (gmt 0)

10+ Year Member



I mean my website.

mcibor

9:56 am on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are two ways of doing that:

1. store the number in file on the server. Onload increment it, on exit decrement. However this way isn't very good (if the connection fails you have no idea that user is not logged in).

2. store the logged status in db. If user logs in then change his status to on and store time of login. If the user logs out then status is off. Change the status if user is on for more than an hour doing nothing.
SELECT COUNT(*) WHERE status='on'; This will give you number of logged users, or you can even print them out.

Best regards
Michal Cibor

kenchix1

3:46 am on Jun 20, 2005 (gmt 0)

10+ Year Member



thanks! :)

any other way to do it? like get status from the server itself?

dreamcatcher

7:43 am on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a number of free user online scripts on the net. Try Hotscripts.com or do a search on Google.

dcrombie

9:45 am on Jun 20, 2005 (gmt 0)



Unless you use a Java or Flash object that constantly talks to the server, then the concept of someone being 'on the site' is a bit meaningless.

Using HTML/PHP, a page is requested and downloaded, then nothing happens until another page is requested. So the best you can do is find out "how many people viewed a page in the last n minutes".

Burner

2:50 am on Jun 21, 2005 (gmt 0)

10+ Year Member



A really simple way to do it would be something like this...

Set up an include that gets run on every page then script something like this.

$sql = "INSERT INTO `activity_table` (tcpip, time, pagename) VALUES ('".$_SERVER['REMOTE_ADDR']."', NOW(), '".$_SERVER['PHP_SELF']."');";
$result = mysql_query("$sql", $linkID);

This would log the tcp/ip address, time, and pagename into your database. Then, to see what's been entered in like the last 15 minutes you could use something like:

$sql = "SELECT * FROM `activity_table` WHERE time >= DATE_SUB(NOW(),INTERVAL 15 MINUTE)";
$result = mysql_query("$sql", $linkID);

This is really simplified, but hopefully the concept helps.

Burner

brendan3eb

3:26 am on Jun 21, 2005 (gmt 0)

10+ Year Member



A friend showed me a function or something that involved sessions that could count the amount there are or something like that and that would do all the work for you. I'll try to remember the method and will post back.