php setcookie时值为null或空字符串(删除cookie)
在php中设置cookie与删除cookie都可以使用php setcookie来实现,如果设置就设置有值,如果删除就设置 cookie value为空或null或时间过期都可以删除,下面我们来看一些实例。
长久以来,在php中删除cookie的时候,都是使用
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string
$domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
$value 随便写, $expire设置为一个已经过去的时间即可。
官方文档中也是这样写的:
http://www.php.net/manual/en/function.setcookie.php
Example #2 setcookie() delete example
When deleting a cookie you should assure that the expiration date is in the past, to trigger
the removal mechanism in your browser. Examples follow how to delete cookies sent in previous
example:
代码如下 | 复制代码 |
// set the expiration date to one hour ago setcookie ("TestCookie", "", time() - 3600); setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1); ?> |
今天遇到一件奇怪的事, 在setcookie的时候,传了一个空字符串给$value,结果竟然是此cookie被删除了
…
代码如下 | 复制代码 |
$name = "post_url"; delete_cookie |
相当不解。
去翻php 5.4.13 的源码:
ext/standard/head.c
代码如下 | 复制代码 |
173 PHP_FUNCTION(setcookie) FAILURE) { domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) { expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC) and 14 */ \t\r\n\013\014'" ); isspace for 13 and 14 */ \t\r\n\013\014'" ); TSRMLS_CC); ""); expires, 0 TSRMLS_CC); |
参数中的value在C语言中的类型是char * , 还有一个 value_len标明了它的长度。
如果value_len为0的话,就写了下面的cookie:
值为”deleted”, 过期时间为 Thu, 01-Jan-1970 08:00:01 CST 或者说是 Thu, 01-Jan-1970 00:00:01
GMT
看来setcookie($name, “”) 确实可以删除这个cookie了…
同理,在php中,strval(NULL) === “” , 所以 setcookie($name, NULL) 也就相当于 setcookie($name,
“”),同样可以删除此cookie.
另外,比较好奇的是:
代码如下 | 复制代码 |
if (value && value_len == 0) { } else { } |
else 中包含了 value 为null 的情况, 这是一种什么样的情况呢?
看来setcookie($name, “”) 确实可以删除这个cookie了…
同理,在php中,strval(NULL) === “” , 所以 setcookie($name, NULL) 也就相当于 setcookie($name,
“”),同样可以删除此cookie.
另外,比较好奇的是:
代码如下 | 复制代码 |
if (value && value_len == 0) { } else { } |
else 中包含了 value 为null 的情况, 这是一种什么样的情况呢?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
