This small PHP script enables counting visitors on a website by storing the visitor count in a text file (count.txt
). The file is locked using flock()
to avoid issues with simultaneous access (race conditions).
- Simple PHP script, easy to integrate into existing websites
- Prevents write conflicts through file locking
- No database required, purely file-based
- Suitable for small to medium websites and blogs (e.g. HTMLy)
- Upload the files
counter.php
and an empty filecount.txt
to your webspace (e.g. Strato). - Make sure
count.txt
is writable (e.g. adjust file permissions). - Include the counter in your website using PHP, e.g.:
- Visit your website and the counter will automatically increment the visitor count.
- Ideally,
count.txt
should be located outside the web root or protected by.htaccess
so that nobody can directly modify the file. - The counter counts every page request, including bots and reloads. For more refined counting, additional mechanisms (cookies, user-agent filtering) would need to be added.
MIT License – see LICENSE file.