Veronica's Hit-Counter:

This script finds/creates and reads/writes a file

Looking for file...
File found!

Checking for readability...
File is readable -- reading the file.
File reads: 34

Checking for writeability...
File is writeable -- writing the file.
Writing 35 to the file.

This page has been visited 35 times.

 

CODE FOLLOWS

<?php
include('includes/header.php');

// set error reporting to all
error_reporting(E_ALL);

echo(
"<h1>Veronica's Hit-Counter:</h1>");

echo(
'<h2>This script finds/creates and reads/writes a file</h2>');

$counterFile '../docs/count.txt';

// does file exist?
echo('Looking for file... <br>');
if(
file_exists($counterFile))
{
    echo(
'File found! <br>');
}
else
{
    echo(
'File is NOT there -- creating a new file. <br>');

    
// create a new counter file
    
$handle fopen($counterFile'w') or die('Could not create file.');
    
fwrite($handle'0');
    
fclose($handle);
}
echo(
'<br>Checking for readability... <br>');
// try reading the file
$handle fopen($counterFile'r');
if(
$handle)
{
    echo(
'File is readable -- reading the file. <br>');

    
// read the contents of the counter file
    
$counter = (int) fread($handle,  filesize($counterFile));
    
fclose($handle);
    echo(
"File reads: $counter<br>");
}
else
{
    echo(
"Cannot read the $counterFile ! <br>");
}
echo(
'<br>Checking for writeability... <br>');
// try writing the file
$handle fopen($counterFile'w');
if(
$handle)
{
    echo(
'File is writeable -- writing the file. <br>');
    
// increment the counter
    
$counter++;

    
// write the new value of the counter to the file
    
fwrite($handle$counter);
    
fclose($handle);
    echo(
"Writing $counter to the file.<br>");
}
else
{
    echo(
"Cannot write to the $counterFile !");
}

echo(
"<h3>This page has been visited $counter times.</h3>");

include(
'includes/footer.php');
?>