1,基于文件实现的php计数器
$cfile = "myData.dat";
$fh = fopen($cfile, "r+");
if (!$fh){
die("
Failed to open file $cfile.");
}
$s = fgets($fh, 6);
$count = (int) $s;
$count = $count + 1;
$count = str_pad($count, 6);
rewind($fh);
fwrite($fh, $count);
echo "$count";
fclose($fh);
?>
2,基于cookie的php计数器
if (!isset($_COOKIE['visits'])) $_COOKIE['visits'] = 0;
$visits = $_COOKIE['visits'] + 1;
setcookie('visits',$visits,time()+3600*24*365);
?>
基于cookie实现的php计数器-www.jbxue.comif ($visits > 1) {
echo("This is visit number $visits.");
} else { // First visit
echo('欢迎来到脚本学堂,开启你的技术之旅吧!');
}
?>