Tedd's Hit-Counter:
This script finds/creates and reads/writes a file
File found!File is readable -- reading the file.
File reads: 645
File is writeable -- writing the file.
Writting 646 to the file.
<?php
include('includes/header.php');
error_reporting(E_ALL); // set error reporting to all
echo("<h1>Tedd's Hit-Counter:</h1>");
echo('<h2>This script finds/creates and reads/writes a file</h2>');
$counterFile = '../doc/count.txt';
if(file_exists($counterFile)) // does file exist?
{
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);
}
$handle = fopen($counterFile, 'r'); // try reading the file
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>");
}
$handle = fopen($counterFile, 'w'); // try writting the file
if($handle)
{
echo('File is writeable -- writing the file. <br>');
$counter++; // increment the counter
// write the new value of the counter to the file
fwrite($handle, $counter);
fclose($handle);
echo("Writting $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');
?>