hello how are yo guys i have this code for count visitors the question is how can i know number of visotors on the all pages on the website not on one main page or one page

i have these table
id
ip
date

oops i am sorry

<?php 



$ip_on=$_SERVER['REMOTE_ADDR'];
$time_on=time();
$sel_on=mysql_query("select * from online where ip='$ip_on'");
$num_on=mysql_num_rows($sel_on);

if ($num_on==0){
$insert_on=mysql_query("insert into online values('','$ip_on','$time_on')");

}else {

$update_on=mysql_query("update online set 
time='$time_on' where ip='$ip_on'

");

}
$time_on_out=$time_on-300;
$select=mysql_query("select * from online where time>'$time_on_out'");
$num_rows=mysql_num_rows($select);
echo $num_rows;
?>

So where is your code? I assume that from a certain page, you are writing that information back to a DB table? If so, why not just include this for all of your pages? Include an additional field in your table for page name.

You do realize that there are free service online to help you with this. For example, have you taken a look at Google Analytics?

Well, my advice is: why reinvent the wheel? Google offers a great application for tracking your website's visitors and it's called Google Analytics. It's free to use. You should try it :).

If you want to keep track of the number of visitors on your website you will have to think about the following: do you want to track unique visitors or do you not? Then you could insert a record for each time a certain IP visits your website, except (in case you want to track only unique visitors) if the IP has already been inserted in the past X hours, for example.

guys i want to know how can i calculate number of visitor on whole pages on the website not only on the main page of website and i use google analytics but i want it also

For pageviews, just insert a new record into a table called, for example "pageviews", including at least the date and an ID, every time a page is loaded. Do this for every page, and if you want to know the total of pageviews for a certain period, then SELECT COUNT(...) FROM pageviews WHERE date BETWEEN ... AND ...

no guys you are not understand me i mean i want to know number of vistors on website"who is online"

Oooooh! Well in that case you could create such a same table and then, instead of counting the total number of page loads in the last x hours, you could do something like:

SELECT COUNT(DISTINCT ip_address) FROM pageloads WHERE time BETWEEN ... AND ...

please minitauros may you give me an exmplae about what did you say for exmple i want to know number of visitor before 5 minute

did you mean as this :
$query=mysql_query("select count(DISTINCT ip) where time between");

Uhuh yea, that's what I mean. Of course you need a table with records to obtain results from. You could also replace the BETWEEN with, for example, something like "WHERE time > NOW() - 60 * 60" to find all users that have been active in the last hour.

well but how can i calculate all users on all website for exmple if i now put this code on main page it will calculate just the visitor on the main page and the user who in another pages will not count them

Sure it will :). You put a code on every page of your website that inserts a record into your pageviews table each time a page is loaded, for example:

INSERT INTO pageviews (ip_address, time_of_pageview) VALUES ("' . $ip_address . '", NOW())

Then, to calculate the number of users that are currently online, you could execute a query like this:

SELECT COUNT(DISTINCT ip_address) AS number_of_users_online FROM pageviews WHERE time_of_pageview > NOW() - 60 * 60

thank you minitauros i appreciate your tired

You're welcome!

To know the number (or some more details) of the visitors that are online may seem a really easy thing to be done but ain’t. First of all you will need a table , lets call it “visitors” that will certainly will have an auto increment ID and a LAST_VISIT that will be a timestamp or int(10) (It may also have FIRST_VISIT , IP , PAGES , REFERRAL or anything more that make sense to you).

When a user first enter your site it doesn’t have a VISITOR_ID in your session. So you add a row for him in visitors table (with LAST_VISIT the current timestamp) and store the last inserted id in your session as VISITOR_ID. Each page the visitor visit you update the LAST_VISIT , but here comes the tricky part. The visitor may exit your site , you can’t catch this with JS because he may close his browser , his operating system , or even the electricity, so ever X seconds (lets say X = 30) you should have an AJAX call that will post to a controller (or even a script). Then that script will know the VISITOR_ID from session and update the LAST_VISIT of the visitors table.

Now to get which visitors are online you just count the number of visitors that have LAST_VISIT the last X (lets say 30) seconds.

The script or controller that is called by AJAX call every X second could return that info if you want to display it in your page , and it will change without reloading.

A small security issue is to validate that the AJAX call is indeed from your site. Of course you should check the headers of that request that have AJAX headers but this is not enough. One more security level could be to export in each page with JS variable the session_id hashed with your own key and then add it as post variable to the AJAX call, in order to validate it. There are many more security layers with AJAX but since that isn’t the topic I will not expand my thoughts.

This approach has a cost , a bandwidth and a memory cost. It all have to do how important is that for you to take that cost. Maybe the day that Web Sockets will work in a unified and standardized way is near, so when that happens we will be able to measure the cost that is needed with Web Sockets to perform such tasks vs with AJAX.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.