1. Home
  2. Computing & Technology
  3. PHP / MySQL

PHP Flat File Counter

By Angela Bradley, About.com

2 of 4

Printing the Current Count

<?php
$File = "counter.txt";
//This is the text file we keep our count in, that we just made

$handle = fopen($File, 'r+') ;
//Here we set the file, and the permissions to read plus write

$data = fread($handle, 512) ;
//Actully get the count from the file

$count = $data + 1;
//Add the new visitor to the count

print "You are visitor number ".$count;
//Prints the count on the page
The comments in the code above tell us what it does. It opens up the file, reads the current count, and then adds 1 more. It then prints this number to the site visitor.

2 of 4

Index: PHP Flat File Counter

  1. Create The Count File
  2. Printing the Current Count
  3. Updating the Text File With The New Count
  4. The Full Code

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Flat File Counter PHP - Write Count to PHP - PHP Simple Counter

©2008 About.com, a part of The New York Times Company.

All rights reserved.