PHPCount - PHP text hit counter

INDEX

COPYRIGHT NOTICE

Copyright 2004-2009 Klemen Stirn. All Rights Reserved.

The PHP Counter may be used and modified free of charge by anyone AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT. By using this code you agree to indemnify Klemen Stirn from any liability that might arise from it's use.

Selling the code for this program, in part or full, without prior written consent is expressly forbidden.

Obtain permission before redistributing this software over the Internet or in any other medium. In all cases copyright and header must remain intact. This Copyright is in full effect in any country that has International Trade Agreements with the United States of America or with the European Union.

DESCRIPTION

PHPcount is a simple PHP text hit counter. It uses flat text database so no MySQL is necessary. It can count hits for multiple pages of your website or even websites on other servers. With the help of cookies it can count unique hits only.

Changes in 1.3
- fixed a bug with counting unique visits over several pages

Changes in 1.2
- improved input parameter checking
- added file locking
- added zero-padding option

Changes in 1.1
- added support for counting unique hits
- no more automatic file creation (for security reasons)
- removed referrer check (not really needed anymore)

REQUIREMENTS

» Installation

Please take 5 minutes time and read installation instructions carefully and completely! This will ensure a proper and easy installation.

If you have problems/questions see the "HELP and Troubleshooting" section further down.

  1. FTP to the public folder of your server (where the rest of your website is; usually called "public_html", "www" or "site") and create a folder where you will install PHPCounter (for example named "counter").
    Example: /public_html/counter
    Corresponding URL: http://www.yourdomain.com/counter

    TIP: If you don't know how to FTP or CHMOD files read my simple FTP and CHMOD tutorial

  2. Upload all PHPCounter files and folders to your server in ASCII mode.

  3. Make sure file test.txt inside "logs" folder is writable by PHP scripts. On Unix (Linux) servers CHMOD this file to 666 (rw-rw-rw-).

That's it. To test if PHPCounter works fine open counter.php?page=test in your browser, for example:
http://www.yourdomain.com/counter/counter.php?page=test

Your browser should show something like:

document.write('1');

Got an error message? A few solutions can be found below under HELP and Troubleshooting.

Using PHP text hit counter

To use PHP Counter on your page(s) you need to:

  1. Create an empty text file for each unique page you want to count visits on. For example create files called page1.txt, page2.txt, mypage.txt, somelongname.txt,... Try using letters (a-zA-Z) and digits (0-9) only.
  2. Upload these text files into the logs folder to your server and make sure PHP scripts have permission to write to these files. On Unix (Linux) servers CHMOD this file to 666 (rw-rw-rw-).
  3. To start counting visitors place this code on your site:

    <script language="Javascript" src="http://www.domain.com/counter/counter.php?page=PAGENAME"><!--
    //--></script>

    Change the src parameter accordingly (to the counter.php on your server)! Change PAGENAME to a name of a text file (without ".txt") you uploaded to the "logs" folder above.

    For example set src to:
    http://www.domain.com/counter/counter.php?page=mypage
    This will log visits into file mypage.txt which must be uploaded into "logs" folder

    TIP: Not sure how to correctly paste Javascript code into your website? See my Cut and paste code into HTML document tutorial.

On index.html you would for example use http://www.domain.com/counter/counter.php?page=index
On someotherpage.html you could use http://www.domain.com/counter/counter.php?page=otherpage
Just don't forget to create empty text files index.txt and otherpage.txt and upload them to the "logs" folder!

Counting UNIQUE hits

If you wish to count unique visits (hits) only, then open file counter.txt in a plain text editor (like Notepad or Wordpad in Windows). Inside find line:
$count_unique = 0;
and change it to:
$count_unique = 1;

By changing the value for $unique_hours = 24; you can set how many hours it takes for a visit to count as unique. For example setting to 24 will count only one visit per browser within 24 hours. If you wish to count one visit per browser per 12 hours you would set it to $unique_hours = 12;

Save changes and upload the edited counter.php file to your server.

Zero-padding (minimum digits to display)

Want your counter to display a minimum number of digits? For example display count 123 as 00123? No problem, open file counter.php in a plain text editor (like Notepad or Wordpad in Windows). Inside find line:
$min_digits = 0;
and change the 0 to the minimum number of digits you want to display. For example to disply minimum 5 digits set it to:
$min_digits = 5;

To disable zero-padding simply change $min_digits back to 0

Save changes and upload the edited counter.php file to your server.

» Upgrading from old versions

Please take 5 minutes time and read upgrade instructions carefully and completely! This will ensure a proper and easy upgrade.

  1. Rename all files inside "logs" from name.log to name.txt
  2. Make sure PHP scripts can write to files inside "logs" folder. On Unix (Linux) servers CHMOD these files to 666 (rw-rw-rw-).
  3. Upload the new counter.php file to your server

That's it, you are ready to go!

» HELP and Troubleshooting

1. What is CHMOD and FTP?

I have prepared a simple FTP and CHMOD tutorial which will help you FTP files to your server and set correct CHMOD settings.

2. I get an ERROR saying "Can't write to the log file, please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!"

PHP script doesn't have permission to write to your file(s). Make sure PHP scripts have permission to write to all files inside "logs" folder. On Unix (Linux) servers CHMOD these files to 666 (rw-rw-rw-). If you are not sure how, see my FTP and CHMOD tutorial.

2. I get an ERROR saying "Invalid log file!"

The script can't find your log file. A few things to check:

  1. Did you create an empty text file? If you are calling the script with counter.php?page=mypage you need to have an empty text file called mypage.txt inside your "logs" folder!
  2. File names are CaSe SeNSiTiVe on most servers! MYPAGE.TXT is not the same as mypage.txt. Make sure your file name is in the correct case.
  3. Did you use any special characters in your file name? Try naming the files only with letters (a-zA-Z) and digits (0-9).

 

Since these scripts are free no support is guaranteed. If you can't get the script to work please go through this readme file again carefully and repeat the installation step-by-step (also delete old files and folders from the server). Also please feel free to post any questions or problems you might have in PHPJunkyard forum!

» Stay updated!

Join my FREE newsletter and you will be notified about new scripts, new versions of the existing scripts and other important news from PHPJunkYard.
Click here for more info

» Please rate this script

If you like this script please rate it or even write a review at:

Rate this Script @ The PHP Resource Index

Rate this Script @ Hot Scripts

» Get more useful FREE scripts!

Looking for more PHP scripts? Here is a list of PHPJunkyard FREE scripts:

 

© Copyright PHP Scripts from PHPJunkyard 2004-2009. All rights reserved.