In this tutorial I will show you a step-by-step guide on how to create a simple hit counter for your website using a text file.
First off we need to create 2 files counter.php which will display and update your hit count and counter.txt which will store your current hit count.
Now we need to open up and start creating the code required for the counter.php file.
We first need to get the current hit count for your site by opening on the text file and reading its contents.
Now having got your current hit count stored in the variable $count we need to increment this value by one to show your hit on the web page, this is done using the following code.
The next line is optional depending on whether you would like your total hit counter to be displayed on your website or if you want it to be an invisible counter.
The last required step for the counter.php file is to save the updated hit count back into the counter.txt file.
We do this by opening the counter.txt file in write mode rather than read-only as before and then writing the variable $count to it and closing the file connection.
Now all that is required is to save and upload the two files counter.php and counter.txt into the same directory and chmod the counter.txt file to 777.
Here's the full code
First off we need to create 2 files counter.php which will display and update your hit count and counter.txt which will store your current hit count.
Now we need to open up and start creating the code required for the counter.php file.
We first need to get the current hit count for your site by opening on the text file and reading its contents.
$fp = fopen("counter.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = fread($fp, 1024);
fclose($fp);
Now having got your current hit count stored in the variable $count we need to increment this value by one to show your hit on the web page, this is done using the following code.
$count = $count + 1;
The next line is optional depending on whether you would like your total hit counter to be displayed on your website or if you want it to be an invisible counter.
echo "Page views: $count";
The last required step for the counter.php file is to save the updated hit count back into the counter.txt file.
We do this by opening the counter.txt file in write mode rather than read-only as before and then writing the variable $count to it and closing the file connection.
$fp = fopen("counter.txt", "w");
fwrite($fp, $count);
fclose($fp);
fwrite($fp, $count);
fclose($fp);
Now all that is required is to save and upload the two files counter.php and counter.txt into the same directory and chmod the counter.txt file to 777.
Here's the full code
<?php
/**
* The counter.txt file stores the information for the hits
* All you need to do is upload the counter.txt file and
* the counter.php file to your website and then
*
* Add this line of code on your page to be counted:
* <?php include "counter.php"; ?>
*/
$fp = fopen("counter.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
// Display the number of hits
echo "Page views: $count";
// To disable output to webpage quote the above line using //
$fp = fopen("counter.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>
/**
* The counter.txt file stores the information for the hits
* All you need to do is upload the counter.txt file and
* the counter.php file to your website and then
*
* Add this line of code on your page to be counted:
* <?php include "counter.php"; ?>
*/
$fp = fopen("counter.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
// Display the number of hits
echo "Page views: $count";
// To disable output to webpage quote the above line using //
$fp = fopen("counter.txt", "w");
fwrite($fp, $count);
fclose($fp);
?>