Please note, this is a STATIC archive of website www.phpjabbers.com from 29 Oct 2018, cach3.com does not collect or store any user information, there is no "phishing" involved.
go top

Make PHP counter

It is always good to know how many visits a web page has. Most of the hosting companies offer great tools for monitoring your web site visitors behavior but often you just need to see how many times a web page has been visited. Using PHP and a plain text file this has never been easier. The example below uses a flat-text database so no MySQL database is needed. I am currently working on a more advanced counter script which gives a lot more details about each visits and I will post it on site when it is done.

First, lets make a new php page and also a count.txt file with just 0 digit in it. Upload the count.txt file on your web server. Depending on server configuration you may need to change the permission for this file to 777 so it is writable by the counter script that we are going to use. Now, lets do the actual PHP code.

<?php
$count = file_get_contents("count.txt");
$count = trim($count);
$count = $count + 1;
$fl = fopen("count.txt","w+");
fwrite($fl,$count);
fclose($fl);
?>


First line opens the count.txt file and reads the current counter value. Then the trim() function is used to remove any new line characters so we have an integer. On the next line we increment the current counter value by one and then using the fopen, fwrite and fclose commands we write the new value to our count.txt file.

Please, note that this counter is not suitable for web pages that have many visitors. A large number of file read/writes may corrupt the counter value. For high traffic web sites you should consider using a MySQL based web counter script.

13 Comments to "Make PHP counter"

  • 3hj4hh2j4h5jj2h5j4j444jhjhg

    3hj4hh2j4h5jj2h5j4j444jhjhg

    April 28, 2017 at 10:56 am

    <script type="text/javascript">
    window.location.href = "youtube.com"
    </script>

  • varunkumarriat

    varunkumarriat

    January 3, 2016 at 13:25 pm

    great tutorial just downloaded javascript timer thanks php jabbers

  • GANDE

    GANDE

    June 16, 2015 at 12:55 pm

    your tutorials are a lot interesting and let me say you are a good teaching which means you know much of what you are teaching others. please the good spirit up. thanks

  • Electronic products

    Electronic products

    February 14, 2014 at 13:51 pm

    this code only print value 1 when refresh the page and my file also get 1 value replace all of my code

  • paz

    paz

    April 28, 2012 at 11:58 am

    rahul you need to make another php file and write there :


    <?php
    include "count.txt"
    ?>

    enjoy:)

  • Pummy Manku

    Pummy Manku

    January 10, 2011 at 17:06 pm

    Thanks for the counter. Do anyone have the code to make a serial no. counter? i.e
    1.
    2.
    3.

    Thank You

  • rahul

    rahul

    March 11, 2010 at 12:42 pm

    i try your Make php counter script , i set the permission, the counter is increasing inthe text file but nothing is display in the php file , how clould i do this. pls help me.

    regards
    rahul

  • Grabeorama

    Grabeorama

    November 9, 2008 at 23:25 pm

    Nice example well explained :)

  • uchral

    uchral

    March 25, 2008 at 10:32 am

    hi thank you :)

  • Jose Beyer

    Jose Beyer

    February 16, 2008 at 17:16 pm

    Do you have a PHP script counter with graphic number images?
    ---------------
    Veselin: we currently do not have but can build a custom one for you

next

Add your comment

Captcha
    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts