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

PHP Flat File Counter

By , About.com Guide

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.
Explore PHP / MySQL
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.