php清空数组cookie,ThinkPHP源码学习 cookie函数 设置 取值 删除

/**

* Cookie 设置、获取、删除

* @param string $name cookie名称

* @param mixed $value cookie值

* @param mixed $option cookie参数

* @return mixed

*/

系统内置了一个cookie函数用于支持和简化Cookie的相关操作,该函数可以完成Cookie的设置、获取、删除操作。

Cookie设置

cookie('author','津沙港湾','3600');

执行代码段$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;

//intval — 获取变量的整数值

setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],

$config['httponly']);

$_COOKIE[$name] = $value;

结果为array (size=4)

'pgv_pvi' => string '3174355968' (length=10)

'PHPSESSID' => string 'u1v6kbhgfn583pl7ku8qs0vk53' (length=26)

'name' => string 'value' (length=5)

'author' => string '津沙港湾' (length=12)

还可以支持参数传入的方式完成复杂的cookie赋值,下面是对cookie的值设置3600秒有效期,并且加上cookie前缀think_

cookie('author','津沙港湾',array('expire'=>3600,'prefix'=>'think_'));

执行代码段

(解析$option数组)$option=array('expire'=>3600,'prefix'=>'think_')$config     = array_merge($config, array_change_key_case($option));

结果为array (size=6)

'prefix' => string 'think_' (length=6)

'expire' => int 3600

'path' => string '/' (length=1)

'domain' => string '' (length=0)

'secure' => boolean false

'httponly' => string '' (length=0)

($name前加一个前缀think_)$name = $config['prefix'] . str_replace('.', '_', $name);

结果为'think_author' => string '津沙港湾'

数组参数可以采用query形式参数

cookie('author','津沙港湾','expire=3600&prefix=think_');

执行代码段

(解析$option字符串)$option='expire=3600&prefix=think_'parse_str($option, $option); //parse_str — 将字符串解析成多个变量

结果为($option变成数组)$option数组输出值

array (size=2)

'expire' => string '3600' (length=4)

'prefix' => string 'think_' (length=6)

$option 参数 分别为整型、字符串、数组 代码段if (!is_null($option)) {

//整型转成数组

if (is_numeric($option)){

$option = array('expire' => $option);

//字符串转成数组

}elseif(is_string($option)){

parse_str($option, $option);

}

//数组合并

$config     = array_merge($config, array_change_key_case($option));

}

支持给cookie设置数组值(采用JSON编码格式保存)

cookie('author',array('津沙港湾','崎沙小香港'));

执行代码段if(is_array($value)){

$value  = 'think:'.json_encode(array_map('urlencode',$value));

}

结果为'author' => string '

think:

["%E6%B4%A5%E6%B2%99%E6%B8%AF%E6%B9%BE","%E5%B4%8E%E6%B2%99%E5%B0%8F%E9%A6%99%E6%B8%AF"]'

注意格式手动调整一下方便阅读json编码格式。

Cookie获取

$value=cookie('author');

执行代码段$value =    $_COOKIE[$name];

if(0===strpos($value,'think:')){

$value  =   substr($value,6);//去掉think:

return array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));

}else{

return $value;

}

结果为author对应的值

array (size=2)

0 => string '津沙港湾' (length=12)

1 => string '崎沙小香港' (length=15)

详细分析$value=cookie('author');解码过程array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));

第一步

stripslashes()函数处理

返回一个去除转义反斜线后的字符串,

第二步

json_decode()函数处理,

接受一个 JSON 格式的字符串并且把它转换为 PHP 变量,

json_decode()中设置true时,按数组格式返回变量,

第三步

array_map()函数处理,

json_decode()函数返回的数组传递给urldecode回调函数进行解码。

最后返回数组。

如果要获取所有的cookie,可以使用:

$value=cookie();

执行代码段if (is_null($name)) {

//这部分代码省略

}elseif('' === $name){//$name为空字符串才行 ===恒等于 判断值和类型都要相等

// 获取全部的cookie

return $_COOKIE;

}

Cookie删除

删除某个cookie的值,使用:

$value=cookie('author',null);

执行代码段if (is_null($value)) {

setcookie($name, '', time() - 3600, $config['path'], $config['domain'],

$config['secure'],$config['httponly']);

unset($_COOKIE[$name]); // 删除指定cookie

要删除所有的Cookie值,可以使用:

$value=cookie(null);//清空当前设定前缀的所有cookie值

cookie(null,'think_'); //  清空指定前缀的所有cookie值

执行代码段if (is_null($name)) {

if (empty($_COOKIE))

return null;

// 要删除的cookie前缀,不指定则删除config设置的指定前缀

$prefix = empty($value) ? $config['prefix'] : $value;

if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回

foreach ($_COOKIE as $key => $val) {

if (0 === stripos($key, $prefix)) {

setcookie($key, '', time() - 3600, $config['path'],

$config['domain'],$config['secure'],$config['httponly']);

unset($_COOKIE[$key]);

}

}

}

return null;

}

代码逻辑判断function cookie($name='', $value='', $option=null) {

$config = array(

);

if ($option不为空) {

if ($option为整型){

$option将整型转为数组

}

elseif ($option为字符串){

$option将字符串转为数组;

}

$option为数组

$option数组合并到$config数组中去

}

if(!empty($config['httponly'])){

ini_set("session.cookie_httponly", 1);

}

if ($name为空null) {

if ($_COOKIE为空){

return null;

}

$prefix = empty($value) ? $config['prefix'] : $value;

if ($prefix不为空) {

删除$prefix的cookie

}

return null;

}elseif('' === $name){

// 获取全部的cookie

return $_COOKIE;

}

$name = $config['prefix'] . str_replace('.', '_', $name);

if ('' === $value) {//$value为空字符串

if(isset($_COOKIE[$name])){

$value =    $_COOKIE[$name];

if(0===strpos($value,'think:')){

//解码$value 等于取值

}else{

return $value;

}

}else{

return null;

}

} else {

if (is_null($value)) {

//删除$name的cookie

} else {

if(is_array($value)){

$value  = 'think:'.json_encode(array_map('urlencode',$value));

}

设置cookie

}

}

return null;

}

更一步简化逻辑判断分支function cookie($name='', $value='', $option=null) {

if ($option不为空) {

$option 参数 分别为整型、字符串、数组 代码段

$option数组合并到$config数组中去

}

if ($name为空null) {

$_COOKIE为空 返回null

$prefix不为空 删除$prefix的cookie

}elseif($name为空字符串){

// 获取全部的cookie

}

$name为某个值

取值

if ($value为空字符串) {

取$name的值

} else {

if (is_null($value)) {

//删除$name的cookie

} else {

设置cookie

}

return null;

}

由上面的逻辑判断if语句可以看出

根据$name=''空字符串(获取全部cookie),

$name=空null, (删除$prefix的cookie)

$name='某个值'(取值|设置值){

$value=''空字符串(取值),

$value=空null(删除$name的cookie),

$value='某个值'(设置值)}

$option 参数 分别为整型、字符串()、数组 三种情况进行设计。

附上Cookie函数(源代码)/**

* Cookie 设置、获取、删除

* @param string $name cookie名称

* @param mixed $value cookie值

* @param mixed $option cookie参数

* @return mixed

*/

function cookie($name='', $value='', $option=null) {

// 默认设置

$config = array(

'prefix'    =>  C('COOKIE_PREFIX'), // cookie 名称前缀

'expire'    =>  C('COOKIE_EXPIRE'), // cookie 保存时间

'path'      =>  C('COOKIE_PATH'), // cookie 保存路径

'domain'    =>  C('COOKIE_DOMAIN'), // cookie 有效域名

'secure'    =>  C('COOKIE_SECURE'), //  cookie 启用安全传输

'httponly'  =>  C('COOKIE_HTTPONLY'), // httponly设置

);

// 参数设置(会覆盖黙认设置)

if (!is_null($option)) {

if (is_numeric($option))

$option = array('expire' => $option);

elseif (is_string($option))

parse_str($option, $option);

$config     = array_merge($config, array_change_key_case($option));

}

if(!empty($config['httponly'])){

ini_set("session.cookie_httponly", 1);

}

// 清除指定前缀的所有cookie

if (is_null($name)) {

if (empty($_COOKIE))

return null;

// 要删除的cookie前缀,不指定则删除config设置的指定前缀

$prefix = empty($value) ? $config['prefix'] : $value;

if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回

foreach ($_COOKIE as $key => $val) {

if (0 === stripos($key, $prefix)) {

setcookie($key, '', time() - 3600, $config['path'],

$config['domain'],$config['secure'],$config['httponly']);

unset($_COOKIE[$key]);

}

}

}

return null;

}elseif('' === $name){

// 获取全部的cookie

return $_COOKIE;

}

$name = $config['prefix'] . str_replace('.', '_', $name);

if ('' === $value) {

if(isset($_COOKIE[$name])){

$value =    $_COOKIE[$name];

if(0===strpos($value,'think:')){

$value  =   substr($value,6);

return array_map('urldecode',

json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));

}else{

return $value;

}

}else{

return null;

}

} else {

if (is_null($value)) {

setcookie($name, '', time() - 3600, $config['path'],

$config['domain'],$config['secure'],$config['httponly']);

unset($_COOKIE[$name]); // 删除指定cookie

} else {

// 设置cookie

if(is_array($value)){

$value  = 'think:'.json_encode(array_map('urlencode',$value));

}

$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;

setcookie($name, $value, $expire, $config['path'],

$config['domain'],$config['secure'],$config['httponly']);

$_COOKIE[$name] = $value;

}

}

return null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值