Visit counter

Good morning,

I have my portfolio in html and css and I would like to have a kind of visit counter for my steps on my web page so that I can know how many visitors have come to my portfolio.

How to do this?

Here is a very basic PHP example…

portfolio.php

<?php
  session_start();
  $counter_name = "counter.txt";

// Check if a text file exists.
// If not create one and initialize it to zero.

if(!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
 }

// Read the current value of our counter file

  $f = fopen($counter_name,"r");
  $counterVal = fread($f, filesize($counter_name));
  fclose($f);

// Has visitor been counted in this session?
// If not, increase counter value by one

if(!isset($_SESSION['hasVisited'])){
  $_SESSION['hasVisited']="yes";
  $counterVal++;
  $f = fopen($counter_name, "w");
  fwrite($f, $counterVal);
  fclose($f);
 }
?>

<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1"
<title>Untitled document</title>

</head>
<body>
 <h1>Test</h1>
 <div id="visitor-count">
  <?php echo "You are visitor number $counterVal to this site";?>
 </div>
</body>
</html>

If my memory serves me well, I believe that the php
was originally coded by our very own James_Hibbard. :winky:

2 Likes

Seems to continue here