PHP中Cookie和Session操作

 

Cookie

设置cookie:

//expire:cookie存在时间
//domain:为一个子域名设置cookie,默认是域下所有子域
set cookie(name, value, expire, path, domain);

实例:

<?php 
$expire=time()+60*60*24*30; 
setcookie("user", "runoob", $expire); 
?>
<html>

函数的调用条件:必须在另输出之前安装
取回cookie的值:

<?php
    echo $_COOKIE["user"];
    print_r($_COOKIE);
?>

得就此isset()函数来检测某个名称的cookie数据是否已经安装:

if (isset($_COOKIE["user"])){
    echo "已经设置cookie";
}

Cookie数据的删减:
就此set
cookie来装有名称的cookie为空,并且有效时间也过去底流年,来达成删除的目的。

<?php
    setcookie("user", "", time()-3600);
?>
<?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].".>";
   }

?>

Session

起来Session会话,必须于任何输出内容前面

<?php session_start(); ?> 
<html>
<body> 

</body>
</html>

存取Session变量

<?
phpsession_start();
// 存储 session 数据 
$_SESSION['views']=1;
?>

<html> 
<head> 
<meta charset="utf-8"> 
</head> 
<body>

<div>
    <?php
    // 检索 session 数据
        echo " ". $_SESSION['views'];
    ?>
</div>

</body> 
</html>

Session变量的绝迹
unset()函数用于释放指定的session变量:

<?php 
session_start(); 
if(isset($_SESSION['views'])) {
    unset($_SESSION['views']); 
}
?>

session_destroy()函数彻底销毁session,并且重置session

  

实例应用

<html>
<div>
    <?php
        $isLogin = false;
        $userinfo = array(
            "xuqian" => "123123",
        ); 
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $username = $_POST['username'];
            $password = $_POST['password'];
            if(!(empty($username) || empty($password))){
                echo "进入---> <br/>";                
                if($password == $userinfo[$username]){
                    $expire = time() + 60*5;
                    setcookie("user","xuqian",$expire);
                    $isLogin = true;
                    echo "post----";
                }
            }else {
                $login = false;
            }
        }
        if(isset($_COOKIE['user'])){
            $isLogin = true;
            echo "cookie --------";
        }
    ?>
</div>
<head>
<meta charset = 'utf-8'/>
<title>测试表单</title>
</head>
<body>

<div>
    <?php
        if($isLogin){
            echo "Wellcome !!!!!!!!";
        }
        else{
            echo "Please Login !!!!";
        }
    ?>
</div>

<form action="#" method="POST">
用户名:<input type="test" name="username"><br/>
密 码:<input type="password" name="password"><br/>
<input type="submit" name="submit" value="提交"/>
</form>

<div>
<?php
    echo "用户名: ";   echo $_POST['username'];echo "<br/>";
    echo "密码: ";    echo $_POST['password'];
    if (!empty($_POST['submit'])){
        if (empty($_POST['password'])){
            echo "NULL";
        }
    }
?>
</div>

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

  

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图