Bakhtiyor
Bakhtiyor

Reputation: 7318

Visitors Counter for simple web sites like Vinaora

Does anybody know any visitor counter for a website which is built without using CMS (Joomla,Drupal) but using simple HTML,CSS,JS,PHP? It would be better to have a counter like Vinaora Visitors Counter for joomla which is available here

Thank you.

Upvotes: 2

Views: 2626

Answers (1)

Zuul
Zuul

Reputation: 16269

Don't know of any that doesn't rely on a database to store the information and present it beautifully to the visitor.

A solution relying on a text file can be performed like this:

<?php

  // file name and file path
  $fileName = 'counter.txt';
  $filePath = dirname(__FILE__).'/'.$fileName;


  /*
   * If the file exists
   */
  if (is_file($filePath)) {

    $fp = fopen($filePath, "c+");            // open the file for read/write
    $data = fread($fp, filesize($filePath)); // ready entire file to variable
    $arr = explode("\t", $data);             // create array

    // run by each array entry to manipulate the data
    $c=0;
    foreach($arr as $visit) {
      $c++;
    }

    // output the result
    echo '<div id="visits">'.$c.'</div>';

    // write the new entry to the file
    fwrite($fp,time()."\t");

    // close the file
    fclose($fp);

  /*
   * File does not exist
   */
  } else {

    $fp = fopen($filePath, "w"); // open the file to write
    fwrite($fp, time()."\t");    // write the file

    // output the data
    echo '<div id="visits">1</div>';

    // close the file
    fclose($fp);

  }

?>

This solution stores the PHP time() of each visit separated by a \t using PHP fwrite(), fread(), fopen() and fclose(). With the stored time, you can then perform some calculations and present a visits board with the details you need.

The above example illustrates all of this but is only showing the total visits.

Upvotes: 2

Related Questions