PHP

Hit counter in PHP

First create a page hitcounter.txt save in the same directory  you will put your hit counter in. put the value in hitcounter.txt where you want to  start your hitcounter .

<?
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0] ++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
$sno=$hits[0];
echo $sno;
?>

1.Set the variable to file hitcounter.txt. Set the number of visits to the current value of the contents of the hitcounter.txt file:

$count_my_page = ("hitcounter.txt");

2. When the script is accessed as the page loads, it not only reads the current number of hits on the page in the hitcounter.txt, it must increase the value by 1 for the current hit to be recorded. Add 1 to your value of $hits and call it $hits

$hits = file($count_my_page);
$hits[0] ++;

3. Open the file that is keeping count so we can write to it

$fp = fopen($count_my_page , "w");

4.Replace the value in the file with the new $hits number after it was incremented.

fputs($fp , "$hits[0]");

5.Close the txt file.

fclose($fp);

6.Set the code to display your hit number.

$sno=$hits[0];
echo $sno;
View Demo
Download Source Code(Hit counter in PHP)
Size: 596 Byte
Version: V 1.0

Recommended Tutorials for you


Related posts

How to delete multiple records in PHP

Anuj Kumar

PHP – PECL

Anuj Kumar

How to check Email and username availabilty live using jquery/ajax

Anuj Kumar

Understanding of php.ini

Anuj Kumar

Types of Errors in PHP

Anuj Kumar

PHP – PEAR

Anuj Kumar

Get City Country By IP Address in PHP

Anuj Kumar

How to insert data into MySql using PHP

Anuj Kumar

PHP Constants

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.