Home > php教程 > php手册 > body text

php中cookie用法详细介绍

WBOY
Release: 2016-06-13 10:15:54
Original
939 people have browsed it

cookie是发送在浏览器端一个小小的文件,可用来记录用户操作过的记录,如访问了那些文件等

 

写入cookie

 代码如下 复制代码

setcookie(name, value, expire, path, domain);

function w_cookie($n, $c, $e = 0,$isdes=1)
 {
   if($isdes==1){$c=endes($c,deskey);}
      $exp = time() + 3600 * 24 * 30;
      if($e == 0)
     {
          setcookie($n, $c, $exp,"/");
     }
     else
     {
      setcookie($n, $c,0,"/");
      }
 }

 
关闭cookie方法就是把它设置过期即可。


例1

写cookie

 代码如下 复制代码

function cookie($var, $value='', $time=0, $path='', $domain=''){
$_COOKIE[$var] = $value;
  if(is_array($value)){
      foreach($value as $k=>$v){
        setcookie($var.'['.$k.']', $v, $time, $path, $domain, $s);
      }
  }else{
        setcookie($var, $value, $time, $path, $domain, $s);
  }
}
 
//调用方法
 
cookie("website","安卓主题","./","www.hzhuti.com");
//

例2

防止重复提交

 代码如下 复制代码

$time = time() + 300;    //5分钟过期
$code = md5($string . $time . $salt);
setcookie('check_time', $time);
setcookie('code', $code);

//验证部分
$TIME = time();
if($check_time     //expire过期
   
if(md5($string . $check_time . $salt) !== $code)

更多关于php cookie用法可参考:http://www.bKjia.c0m/tags.php/php%20cookie/

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template