This free page counter php script uses the fopen()
, fread()
, fwrite()
, and fclose()
functions to read and update a text file that stores the current value of the counter. The text file “counter.txt” must be created and initially set to zero before this script can be used.
<?php
// Open the text file
$file = fopen("counter.txt", "r");
// Read the current value of the counter
$count = fread($file, filesize("counter.txt"));
// Close the file
fclose($file);
// Increment the counter
$count = $count + 1;
// Open the file for writing
$file = fopen("counter.txt", "w");
// Write the new value to the file
fwrite($file, $count);
// Close the file
fclose($file);
?>
<html>
<head>
<title>Website counter</title>
<style>
.counter {
text-align: center;
position: fixed;
bottom: 20px;
right: 20px;
background-color: #f5f5f5;
padding: 20px;
border-radius: 5px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.counter p {
font-size: 20px;
margin-bottom: 5px;
color: #333;
}
.counter span {
font-size: 36px;
font-weight: bold;
color: #4CAF50;
}
.counter:hover {
background-color: #e5e5e5;
cursor: pointer;
}
</style>
</head>
<body>
<div class="counter">
<p>Visitors:</p>
<span>232323<?php echo $count; ?></span><br />
<u><a href="https://php.org" title="PHP tutorials">Powered by PHP.org</a></u>
</div>
</body>
</html>