ѡ̳

 找回密码
 ע
搜索
查看: 311|回复: 0
打印 上一主题 下一主题

thinkphp公共函数之cookie函数

[复制链接]

789

主题

1158

帖子

4197

积分

Ա

Rank: 9Rank: 9Rank: 9

积分
4197
跳转到指定楼层
¥
发表于 2017-11-12 19:10:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
源码

  1. /**
  2. * Cookie 设置、获取、删除
  3. * @param string $name cookie名称
  4. * @param mixed $value cookie值
  5. * @param mixed $option cookie参数
  6. * @return mixed
  7. */
  8. function cookie($name='', $value='', $option=null) {
  9.     // 默认设置
  10.     $config = array(
  11.         'prefix'    =>  C('COOKIE_PREFIX'), // cookie 名称前缀
  12.         'expire'    =>  C('COOKIE_EXPIRE'), // cookie 保存时间
  13.         'path'      =>  C('COOKIE_PATH'), // cookie 保存路径
  14.         'domain'    =>  C('COOKIE_DOMAIN'), // cookie 有效域名
  15.         'secure'    =>  C('COOKIE_SECURE'), //  cookie 启用安全传输
  16.         'httponly'  =>  C('COOKIE_HTTPONLY'), // httponly设置
  17.     );
  18.     // 参数设置(会覆盖黙认设置)
  19.     if (!is_null($option)) {
  20.         if (is_numeric($option))
  21.             $option = array('expire' => $option);
  22.         elseif (is_string($option))
  23.             parse_str($option, $option);
  24.         $config     = array_merge($config, array_change_key_case($option));
  25.     }
  26.     if(!empty($config['httponly'])){
  27.         ini_set("session.cookie_httponly", 1);
  28.     }
  29.     // 清除指定前缀的所有cookie
  30.     if (is_null($name)) {
  31.         if (empty($_COOKIE))
  32.             return null;
  33.         // 要删除的cookie前缀,不指定则删除config设置的指定前缀
  34.         $prefix = empty($value) ? $config['prefix'] : $value;
  35.         if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回
  36.             foreach ($_COOKIE as $key => $val) {
  37.                 if (0 === stripos($key, $prefix)) {
  38.                     setcookie($key, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
  39.                     unset($_COOKIE[$key]);
  40.                 }
  41.             }
  42.         }
  43.         return null;
  44.     }elseif('' === $name){
  45.         // 获取全部的cookie
  46.         return $_COOKIE;
  47.     }
  48.     $name = $config['prefix'] . str_replace('.', '_', $name);
  49.     if ('' === $value) {
  50.         if(isset($_COOKIE[$name])){
  51.             $value =    $_COOKIE[$name];
  52.             if(0===strpos($value,'think:')){
  53.                 $value  =   substr($value,6);
  54.                 return array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));
  55.             }else{
  56.                 return $value;
  57.             }
  58.         }else{
  59.             return null;
  60.         }
  61.     } else {
  62.         if (is_null($value)) {
  63.             setcookie($name, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
  64.             unset($_COOKIE[$name]); // 删除指定cookie
  65.         } else {
  66.             // 设置cookie
  67.             if(is_array($value)){
  68.                 $value  = 'think:'.json_encode(array_map('urlencode',$value));
  69.             }
  70.             $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
  71.             setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
  72.             $_COOKIE[$name] = $value;
  73.         }
  74.     }
  75.     return null;
  76. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | ע

本版积分规则

QQ|Archiver|ֻ|С|ѡ̳

GMT+8, 2026-5-2 03:19 , Processed in 0.086451 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表
0.1015s