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])
	}
?>

  

posted @ 2018-08-22 15:57  ®Geovin Du Dream Park™  阅读(338)  评论(0编辑  收藏  举报