Facebook Twitter Reset

This Tutorial Has Been Viewed 6,578 Times.

Hit counter using php

PHP Hit Counter

Welcome, this tutorial will guide you on how to make a PHP hit counter.

Requirments: You should know the basics to a file system unit.

Step 1: We need to create a script called ‘counter.php’ and a text file called ‘hits.txt’ WITH a value of 0 [You Just Create A Text File And Put Number ZERO On That And Save It], if you are not doing this your counter script wont work.Make sure the TXT file it has enough permissions.

Review: You should now have 2 files open, ‘counter.php’ open with nothing in it and ‘hits.txt’ open with a ’0′ in it.

Step 2: CLOSE ‘hits.txt’ (We don’t need it any more for now) Now we need to start PHP coding, so first we do is enter the basic page outline as used in most php pages.

 

PHP Code:

<?php

?>

Review: You should now have a PHP file called ‘counter.php’ open with the code above.

Step 3: Now we need to have the file get the code from the page, so we need to open it and get the data, this is outlined below.

 

PHP Code:

$open fopen(“hits.txt”“r+”);
$value fgets($open);
$close fclose($open);

$value++;

Review: You now can read the file, and it will double, hence the ‘$value++;’, this adds one to the current file.

Step 4: Now we should output the new number to the file. This is the easy part.

 

PHP Code:

$open fopen(“hits.txt”“w+”);
fwrite($open,$value);
fclose($open);

Review: Now we have the number plus one in the file ‘hits.txt’.

Step 5: Let’s echo the value so that it can be included anywhere.

PHP Code:

echo $value;

Final Talk: Ok, so now your file should look like this:

PHP Code:

<?php

$open fopen(“hits.txt”“r+”);
$value fgets($open);
$close fclose($open);

$value++;

$open fopen(“hits.txt”“w+”);
fwrite($open$value); // variable is not restated, bug fixed.
$close fclose($open);

echo $value;

?>

VN:F [1.9.13_1145]
Rating: 6.8/10 (9 votes cast)
VN:F [1.9.13_1145]
Rating: +2 (from 6 votes)
Hit counter using php, 6.8 out of 10 based on 9 ratings

Incoming search terms:




You will also be interested in ,

9 Responses to “Hit counter using php”

  1. April 23, 2009 at 7:50 am #

    thanks

    VA:F [1.9.13_1145]
    Rating: 4.7/5 (3 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  2. May 5, 2009 at 8:50 am #

    easy to understand, thanks

    VA:F [1.9.13_1145]
    Rating: 3.0/5 (2 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  3. May 11, 2009 at 5:51 am #

    No matter which way I enter code to read a text file or write a text file, for some reason my counter.php won’t read or write to that file. I have used 4 different ways to code it and I always end up printing 1 view. I tried debugging before I update the counter and $value never gets a value to it, just empty.

    VA:F [1.9.13_1145]
    Rating: 1.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  4. May 11, 2009 at 12:00 pm #

    Dear Daniel,

    First check if there is any extra charecters in your code when you tried to copy and paste the code above.

    Second have you checked the permissions of the TXT file you are trying to R/W with the php script.

    Third if none of the above worked pls paste the code here.I will try to rectify the issue.

    Thanks.

    VN:F [1.9.13_1145]
    Rating: 1.0/5 (1 vote cast)
    VN:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  5. zain
    November 18, 2009 at 12:58 am #

    hi, where do i put the php code, most of my website is in html but i’m using IIS php 5.2 Fast CGI. I dont want my hits displayed on my website, any advice, Just want it for personal reasons. Just started a blog.
    please help

    VA:F [1.9.13_1145]
    Rating: 1.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
    • November 19, 2009 at 9:30 pm #

      Hello Zain,
      Since you are using html codes, i suppose all your files will have .html extension.So what you need to do is since your server support php you can do like this
      1)Create a PHP file named hit.php
      2)Copy and paste the entire code i have given in this post except echo $value
      3)add an iframe in your html code
      4)Call the hit.php from the iframe.If you want you can give the height and width of the IFRAME as 0%
      4)This will get your counter hidden from others.
      This will help you i believe.Do visit my website for more tricks thanks.

      VN:F [1.9.13_1145]
      Rating: 5.0/5 (2 votes cast)
      VN:F [1.9.13_1145]
      Rating: +1 (from 3 votes)
  6. May 22, 2011 at 5:39 pm #

    Its too simple.Just opening the txt file and incrementing var and closing the fileThats it.I didn’t thought that adding counter is so simple through PHP..Great! I will definitely try this.Thanks for sharing.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -2 (from 2 votes)
  7. ngonidan
    November 7, 2011 at 4:07 pm #

    If you get error like : Warning: fopen(“hitstxt”) [function.fopen]: failed to open stream: No error in C:\wamp\www\test\counter.php on line 3

    It’s because when you copied and pasted the code, the quotes are pasted as “hits.txt” instead of “hits.txt” in notepad.

    Solution, just replace all “ with ” and, ” with “.

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Trackbacks/Pingbacks

  1. Jasmine Pearls - February 13, 2009

    [...] Hit counter using php | Tutorials, Scripts, Technology and … [...]

Leave a Comment

Spam protection by WP Captcha-Free

Affordable Seo PackagesSeo BlogEdu Backlinks
More in Mysql, PHP (35 of 129 articles)