PHP7.27: Cookie and Session
<?php
// 有的浏览器不支持Cookie,这要考虑的
$cFile="count.txt";
$acctime=time();
if(file_exists($cFile)){
$fp=fopen($cFile,"r");
$str=fgets($fp,22);
fclose($fp);
$count=trim($str);
$count++;
}else{
$count=1;
}
$first=false;
if(!isset($_COOKIE['acctime'])){
setcookie("acctime",$acctime,time()+3600*24);
$first=true;
$acctime=3600*24+$acctime;
}else{
$acctime=3600*24+$_COOKIE['acctime'];
}
if($first||$acctime<=time()){
$count=sprintf("%d",$count);
$fp=fopen($cFile,"w");
fputs($fp,$count);
fclose($fp);
}else{
$count--;
}
//print "您是第 ".$count." 位访客。您访问计入统计的时间是:".date("Y-n-j H:i:s",$acctime)."。";
?>
<?php
//读取显示
$f_open=fopen('count.txt','r+');
$count=fgets($f_open);
settype($count,"string");
$len=strlen($count);
$str=str_repeat("0",6-$len);
echo "当前的访问量为:";
for($i=0;$i<=strlen($str);$i++){
echo '<img src=images/0.gif>';
}
for($j=0;$j<$len;$j++){
switch ($count[$j]){
case "0"; $img[$j]="0.gif";break;
case "1"; $img[$j]="1.gif";break;
case "2"; $img[$j]="2.gif";break;
case "3"; $img[$j]="3.gif";break;
case "4"; $img[$j]="4.gif";break;
case "5"; $img[$j]="5.gif";break;
case "6"; $img[$j]="6.gif";break;
case "7"; $img[$j]="7.gif";break;
case "8"; $img[$j]="8.gif";break;
case "9"; $img[$j]="9.gif";break;
}
echo "<img src=images/".$img[$j]." title=".$img[$j].".>";
}
?>
<?php
session_start(); //创建会话
$f_open=fopen('count.txt','r+');
$count=fgets($f_open);
if($_SESSION[count]==''){ //全局变量
$count++;
rewind($f_open);
fwrite($f_open,$count);
fclose($f_open);
$_SESSION[count]=1; //存储会话变量
//unset($_SESSION[count]); //注销会话变量
//session_unregister($_SESSION[count]) //php4
//session_unset($_SESSION[count])
}
?>
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号