PHP/Components/Page Counter

Материал из Web эксперт
Перейти к: навигация, поиск

File based page counter

<!-- Create an empty file myData.dat first -->       
       
       
<?php
$cfile = "myData.dat";
$fh = fopen($cfile, "r+");
if (!$fh){
    die("<BR>Failed to open file <I>$cfile</I>.");
}
$s = fgets($fh, 6);
$count = (int) $s;
$count = $count + 1;
$count = str_pad($count, 6);
rewind($fh);
fwrite($fh, $count);
echo "$count";
fclose($fh);
?>



Use cookie to create page counter

<?php
if (!isset($_COOKIE["visits"])) $_COOKIE["visits"] = 0;
$visits = $_COOKIE["visits"] + 1;
setcookie("visits",$visits,time()+3600*24*365);
?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?php
if ($visits > 1) {
  echo("This is visit number $visits.");
} else { // First visit
  echo("Welcome to my Website! Click here for a tour!");
}
?>
</body>
</html>