| Nucleus Login | Lost password? | Close |
|---|---|---|
| Username (email): | ||
| Password: | ||
|
|
||
|
|
||
PHP: Add a visitorcounter
Problem
How can I show the amount of visitors my site has?
Article ID: #KB00035
Number of views: 2455x
Created: 19 May 2008
Latest change: 19 May 2008
Number of views: 2455x
Created: 19 May 2008
Latest change: 19 May 2008
Solution
You can easily create a visitor-counter (also called hitcounter or counter) in PHP in the following matter:First you create a file called "visitors.txt", with content 0 (the figure, zero)
You chmod this file to 666 via your FTP application. (Connect to the FTP server and rightclick the file, choosing "File Attributes", and enter 666).
Afterwards, you can use the following code on your page to retrieve the amount of visitors:
<?php $teller = "visitors.txt"; $aantalbezoekers = file_get_contents($teller); $aantalbezoekers++; $tellerbestand = fopen($teller, "w"); fwrite($tellerbestand, $aantalbezoekers); fclose($tellerbestand); echo "Totaal aantal bezoekers: " . $aantalbezoekers; ?>
The code above will add 1 to the amount of visitors in "visitors.txt", and display this number on the website.
