• home
  • forum
  • my
  • kt
  • download
  • Simple Page Hit Counter

    Author: 2008-08-28 11:18:58 From:

    This tutorial will teach you how to make a basic page hits counter for your website. It will show you and your visitors how many times your website has been visited

    Table of contents:

       1. The concept
       2. The code
       3. Implementing it

    Requirements:

        * Server with PHP capabilities.
        * Basic PHP knowledge.

    1. The concept
    In this tutorial we will be creating a very basic page hit counter. It will show you and your visitors how many times your websites main page has been loaded. This tutorial will also teach you how to use the file_get_contents() and file_put_contents() functions to retrieve and modify the contents of files.

    The concept of making a page hit counter for your website is very simple. It will be based on a text file which will contain a number of page hits. This number will be constantly increased by 1 on each load of the page. This counter will record every single hit, so keep in mind that the number it shows is not the actual number of visitors, but the number of times the main page has been loaded.

    The two main functions we will be using are: file_get_contents() and file_put_contents(). The result of these two functions is pretty obvious: file_get_contents() returns the data and file_put_contents() saves the data.

    The first required argument of file_get_contents() is the name of the file you wish to retrieve the data from. There are more arguments, but they are not required. For example: You can change the encoding, make it read the file from the include path, change the offset where the reading starts, set the maximum length of data you want to read.

    There are two required arguments in file_put_contents(). The first one is the name of the file you will be writing data to. The second argument is the data you will be writing. There are 2 more arguments that are not required. For example: You could lock the file while it is being written to, change the encryption, set it to be written in binary mode.

    Before continuing, make sure there is a file called 'hits.txt' in the same folder the script will be in and that it is chmod'ed to 777.

    2. The code

    The main code.

    First of all let's get the current number of hits.


    $hits = file_get_contents('hits.txt');


    Now let's add 1 to the number of hits by using the += operator. This operator means add the number on the other side to the variable.


    $hits += 1;



    Now let's save the new number to the 'hits.txt' file.


    file_put_contents('hits.txt', $hits);



    3. Implementing it
    Basically that's all. You can now use the $hits variable anywhere in your page. I would only recommend putting it in the main page if you're going to use it just like this so the results would be more accurate. You can also make a cookie (or add the IP to a database) so it would only show unique hits or make PHP generate an image with the GD library.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (27)
      Cookies and Sessions (11)
      Counters (15)
      Database Related (20)
      Date and Time (13)
      Development (19)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot