Website Visits count

Can anyone share code to count every visit to a website. I don’t want to use the embedded ones available online. Just a simple coded to do incremental count as users visit my site,

When I searched the internet for something like that I learned that visit has many meanings. A person can view many pages in a website. So is that just one visit or multiple visits?

How do you define visit? If you are not sure then you can find many existing articles to help you decide.

multiple visits…i want every visit to all pages counted.

That does not define what a visit is.

I just want every time some loads my website, it counts. or in another word every time someone views my website it counts.

Are you looking for something like this or do you want to code something yourself?

This one works well.

Just getting a count of page requests is rather worthless. Your server should already be logging every request with plenty of details. The who, what, where and when and how. Just use a server log analyzer. Here is an example of what you can get from your logs…

1 Like

something i code myself.

You’re not going to do it in HTML or CSS. You’ll need a server-side script.

Please supply more details about the site.

does it use Apache2, etc.

If you use PHP and have a common footer or header for every page then a simple file update should suffice.

I use php and i want to have the results displayed on footer.

Well this might do the trick

$file = 'count.txt';
if (!file_exists($file)) {
  $fp = fopen($file, "w+");
  fwrite($fp, "0");
$fp = fopen($file, "r+");
flock($fp, 1);
$count = fgets($fp, 4096);
fseek($fp, 0);
fputs($fp, $count);
flock($fp, 3);
echo $count;
1 Like

ill need to create count.txt file? no coontent in it?

No, the script does that for you first time it executes.

It depends on your php ability, as the previous answer says you can update a text file - or you could have a database, say mysql or similar then a php script can be included on every page that will tell you which pages have been visited and how many times, even date and time. But why not simply register with Google Analytics, it’s free, you include a short js on each page and you get all sorts of metrics.

1 Like

Sorry - just seen you want to display results in footer - for me - php / mysql database, but ask yourself do you really want to display this publicly and if so, why?

i am new in this, do i need to enlose the script in <? php script ?>

Yes. Any time you embed PHP into a file, it must be surrounded by opening and closing PHP tags, even if the PHP is the only thing in the file.

1 Like

Don’t forget the file must have a PHP extension, .html will not work.