Hi, programmer
How to count the number of the time and the page has been acessed?
Free and Open Source PHP counter scripts I recommended can only count the number of times the page has been accessed. It cannot count the number of unique visitors. The PHP hit counter simply updates a text file, which keeps track of the number of times the page is accessed. The PHP Script read the current the file, then adds one to the number, writes the number to the file and close the file. The script is then embedded into the html on the web page so that it is executed each time the page is accessed.
The hit counter script looks like this:
The filename variable holds the name of the text file with the hit count in it. When you add the script to your web page, you should also add a text file called "hits.txt" with only the number "0" in it to the directory where your page is held.
The $count variable accesses the value with the "file" function. This function reads the entire file into an array. In this case the array will only have one value. The line "$count[0]++;" increases the value by one.
The file is then opened with the "fopen" function. The "w" in that function allows the file to have write access. If the file is not there, it will return the text "Cannot find hits.txt" and the script will end. If the file is opened successfully, the "fputs" function will write the new $count value to the file. The file is then closed with the "fclose" function.
The last line of the script, "echo $count[0];" writes the hit count to the screen. If you don't want the hit count to be visible, you can either leave that line out, or comment it out by placing a # symbol at the beginning of the line.
Embedding the Code
# Save the code as "hits.php" and place it in the same directory as your web page. If your web server is a Linux server, you should make sure both the "hits.php" is world executable and the "hits.txt" file is world writable by typing the commands "chmod 755 hits.php" and "chmod 777 hits.txt" at a command prompt.
Call the "hits.php" script from within your web page by placing the following line into the html code:
<?php include("hits.php"); ?>
You can test your code by reloading your web page. If the counter increases, you know that you have set everything up correctly. If it does not, check your code for typos and make sure there is a semicolon at the end of each line.
How to count the number of the time and the page has been acessed?
Free and Open Source PHP counter scripts I recommended can only count the number of times the page has been accessed. It cannot count the number of unique visitors. The PHP hit counter simply updates a text file, which keeps track of the number of times the page is accessed. The PHP Script read the current the file, then adds one to the number, writes the number to the file and close the file. The script is then embedded into the html on the web page so that it is executed each time the page is accessed.
The hit counter script looks like this:
PHP Code:
<?php
$filename = "hits.txt";
$count= file($filename);
$count[0]++;
$file = fopen ($filename, "w") or die ("Cannot find $filename");
fputs($file, "$count[0]");
fclose($file);
echo $count[0];
?>
The $count variable accesses the value with the "file" function. This function reads the entire file into an array. In this case the array will only have one value. The line "$count[0]++;" increases the value by one.
The file is then opened with the "fopen" function. The "w" in that function allows the file to have write access. If the file is not there, it will return the text "Cannot find hits.txt" and the script will end. If the file is opened successfully, the "fputs" function will write the new $count value to the file. The file is then closed with the "fclose" function.
The last line of the script, "echo $count[0];" writes the hit count to the screen. If you don't want the hit count to be visible, you can either leave that line out, or comment it out by placing a # symbol at the beginning of the line.
Embedding the Code
# Save the code as "hits.php" and place it in the same directory as your web page. If your web server is a Linux server, you should make sure both the "hits.php" is world executable and the "hits.txt" file is world writable by typing the commands "chmod 755 hits.php" and "chmod 777 hits.txt" at a command prompt.
Call the "hits.php" script from within your web page by placing the following line into the html code:
<?php include("hits.php"); ?>
You can test your code by reloading your web page. If the counter increases, you know that you have set everything up correctly. If it does not, check your code for typos and make sure there is a semicolon at the end of each line.
Comment