Skip to main content

Simple Hit counter

A hit counter will let us know how many times a page is accessed. Incase one visitors loads the page several times, the hit counter willincrease several times (but this is likely to happen only a few times).

The code for the hit counter bellow will save the number of hits in afile named counter.txt(the name of this file may be changed). Each time the page is loaded,the file will be read, the number will be increased by one and the newnumber will be saved to the same file.

Counter.php
<?php


//The file where number of hits will be saved; name may be changed;p.e. "/counter_files/counter1.txt"
$counterfile = "counter.txt";


// Opening the file; number of hit is stored in variable $hits
$fp = fopen($counterfile,"r");
$hits= fgets($fp,100);
fclose($fp);


//increading number of hits
$hits++;

//saving number of hits
$fp = fopen($counterfile,"w");
fputs($fp, $hits);
fclose($fp);

//printing  hits; you may remove next line (and keep the counteronly for your records)
print $hits;

?>
To use this code, copy itto your page in the exact position where you want to show number ofhits. If your page is a ".html" page, change the extension to ".php".Them, visit your page. In the first visit, you will get a couple oferrors (the file where number of hits are recorded does not exists, sosome errors will be shown in the page). Reload the page and you willsee no errors. The hit counter will be working.

In case you want to use different hit counters for different pages,chage name of $counterfile for each page (p.e.: counter1.txt,counter2.txt, etc). 

By phptutorial.info

Comments

  1. Yeah adding hit counter to your site is very important.It not only reports your traffic numbers, but also gives you valuable information that will help you understand the behavior of your site visitors better and so perform outstanding customer intelligence.It helps you rediscover your site and see it with the eyes of your visitors. Simple hit counter tools are available in order to help you track the activities of customers instantly without any hassle.
    http://counterforsite.com/simple-hit-counter/

    ReplyDelete

Post a Comment

Popular posts from this blog

Beginner Html Lesson 2

In the previous lesson we just copy a code and paste it. In this lesson we will guide you through rest of the things what is that let's talk more. We made a webpage with something very weird for beginner don't worry go ahead. <html> : This is the essential thing for any web page and also closing tag </html>. Between these codes we add some more codes <head> </head> , <body> </body> between the body code actually body itself saying that i am body of your webpage. What Head tags containg (<head> </head>) ? Basically head tags contains following <title> <base> <link> <meta> <script> <styles>  What Body tags contains (<body> </body>) ? And a body tags contains anything like your bolded text , your content of any type.Paragraphs , Headings anything. I hope you understand.

Submit Buttons in Html Forms

Submit buttons are use to submit the forms whenever user registration is needed submit buttons always used. How to make those submit buttons? Just simple you can make submit buttons if you know the <input> tag then make it's type= submit value="some name" just like submit, send etc. Submit Buttons Code: <input type="submit" value="Submit" /><br /> <input type="submit" value="Send" /><br /> <input type="submit" value="Submit Form" /><br /> Submit Buttons Example: Submit Buttons in Action Code: <form method="post" action="mailto:pateltutor1@gmail.com" > Name:<input type="text" name="First" size="12 maxlength="12" /> Email:<input type="text" name="Last" size="24" maxlength="24" /> <input type="submit" value="Send Email" /> </form> ...

Reset Buttons in Html Forms

Reset Buttons: Reset buttons are use to reset the forms in Html. Sometime users put wrong information then the users don't be worry of removing whole the fields and fill them again just press the reset button and whole the form back to their original state. That's very useful for the user. Html Code: <input type="reset" value="Reset" /> <input type="reset" value="Start Over" /> Html Code: Reset Button in Action: Now we are giving some action for your understanding what reset button done don't panic we give some form tags and other just visit Forms in Html you will also learn how to make it. Reset Button Code: <form action="xyz.php" method="post"> First Name: <input type="text" size="20" maxlength="12" /><br /> Last Name: <input type="text" size="24" maxlength="24" /><br /> <input type="reset" value=...