|
Here we will go through the creation of the simple hit counter to place on your web pages. It will not use any images or databases, that's why it's called "Simple hit counter".
OK, enough talks, lets begin...
1. Create a counter text file of the name hitcounter.txt and save it to the same directory you will put your hit counter in.
2. Open your php editor. Save the document as counter.php. Set your opening statement for PHP script.
PHP code: <?php ?>
3. Set the variable for the file named hitcounter.txt and place this between your PHP tags. remember to place the filename in quotes. place the semicolon on the end of the line to end the command.
PHP code: <?php $count_my_page = ("hitcounter.txt"); ?>
4. Set the number of visits to the current value of the contents of the hitcounter.txt file:
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); ?>
5. 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.
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; ?>
6.Open the file that is keeping count so we can write to it
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); ?>
7. Replace the value in the file with the new $hits number after it was incremented.
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); ?>
8. You must close the file next.
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); ?>
9. Set the code to display your hit number.
PHP code: <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); ?>
10. Save your php file in same directory as the text file.
11. To use this script you just have to add a php statement that calls that php file. Use the following include:
PHP code: <?php include ("counter.php"); ?>
12. Enjoy :D
|