欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP7.27: Cookie and Session

程序员文章站 2022-04-24 15:50:53
'; } for($j=0;$j"; } ?> ......

 

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