zoukankan      html  css  js  c++  java
  • 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])
    	}
    ?>
    

      

  • 相关阅读:
    一个JAVA题引发的思考
    eclipse好玩的插件集(一) CKEditor插件
    Log4J使用实例---日志进行邮件发送或是存入数据库
    log4j输出到数据库(输出自定义参数、分级保存)
    String和StringBuffer的一点研究
    String、StringBuffer、StringBuilder区分和性能比较
    最新eclipse安装SVN插件
    jsoup select 选择器
    网页导出excel文件
    Dom4j完整教程
  • 原文地址:https://www.cnblogs.com/geovindu/p/9518245.html
Copyright © 2011-2022 走看看