Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Counting the number of Visitors to my pages

I have several pages in PHP. I want to know how many visitors are browing these pages at any point of time. This information would be displayed on the opening page. How do I do it? Please can you give me the code for getting this done?
1 Solution
U must insert a counter code on each php page. So the counter code will collect data about your visitors and write into file (for example counter.txt). On the main you must insert another script to get data from file and display on the screen.
chaiti_cAuthor Commented:
But the counter should decrease as the visitor leaves the page and increase as soon as another user enter. How do I do it???
I have MySQL database as the backend
OK, you must generate unique id for every visitor and put this id and time into database. Do this for each page.

On the main page your script must calculate all visitors (using unique id), who visited your site for example last 30 min (or 5 min).

Also u can use sessions besides unique id.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Try a site like

Its much better choice. cause they have everything setup, and your server doesnt get burdened.

It is tricky - It's easy to know when someone starts to look at a page, but if they close the browser, or go to another website, how can you know they have left???
chaiti_cAuthor Commented:
No problem in generating a unique number for each visitor but how do I trap when a visitor enters my site and also keep a track of when the visitor leaves. What is the code for trapping the visitor when he enters the site and also to trap the moment he leaves.
Can someone please send the code???
There is no need to use any difficult scripts. When a visitor enters your page, you just generate a unique id and send cookie with this unique id, so when user enters another page on your site you get this cookie and insert into the database (insert: unique id, time and page). So you always know which page the visitor enters.

But when the visitor leaves your site ... ;) - this is a big problem to the script. So to resolve it display only those users who visited your site (or some page) last 30 min.

chaiti_cAuthor Commented:
I need to give the exact number of visitor at any given point of time and so just checking how many visitors there are in the last 30 minutes won't help.
Also u can use something like this:
<Body onUnload="'leaves.php')">

// leaves.php starts
   $counter --;

<script language="javascript">
   window.close ();
// ends

This method works good only for u but for your visitors...
It's imposible... :(
All counters over the world use its own algorithm to calculate visitors numbers, but noone can do it correct :(
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now