Home php教程 php手册 php cookie无法保证解决方法

php cookie无法保证解决方法

Jun 13, 2016 am 09:55 AM
cookie php ensure exist client method server Notice of program solve

cookie存在客户端,跟服务器没有关系的,注意你程序里大小写问题,linux是大小写敏感的,还有php教程的配置,比如是否   register_globals   =   Off   的配置在两边都一样等等。

PHP 的 @、# 符号的意思

function foo($n)
{
    $result = 1/$n;
    return $result;
}
echo @foo(0); // 函数中会产生除 0 错误,但加上 @ 后并不显示该错误。
echo "end"; // 输出 end
# 注释符号

同 // 一样,# 是单行注释符(多行注释符为 /* */)。

由于使用了@setcookie,即便在写入cookies是发生错误,也不会输出,造成了无法发现问题。最后把@setcookie改成 setcookie,程序输出如下错误信息:

Warning: Cannot modify header information - headers already sent by (output started at

经过上网一查,发现原来在进行setcookie设置前不能有任何输出内容,然后就去检查代码,也没有发现在setcookie之前输出了东西,在搜索了一下,发现了问题所在,具体内容如下:

今天在WordPress中文论坛逛了一圈。坛子里人气不高,不过还是有很多高手的。会编写插件和模版的高手和连编辑文件都不会的初学者混在一起,论坛就是这样,哈哈。

看到好几个帖子里提到同一个错误,比如这个帖子里提到的:"Warning: Cannot modify header information - headers already sent by (output started at c:program fileseasyphp1-8wwwwp-config.php:1) in c:program fileseasyphp1-8wwwwp-login.php on line 9"

这是一个很典型的问题。WordPress的程序执行时会首先调用wp-config.php一类的配置文件,也会调用wp-db.php建立数据库教程连接以备后用。这些文件只是做一些设置,并不输出html代码。设置完了后,程序本身开始执行了,有些程序会使用header命令设置一个HTTP头。由于HTTP头必须在html代码输出之前设置好,否则html代码已经开始往客户端发送了,HTTP也就已经发送过了,没法追回来重新设置了。WordPress CodeX里对这个问题作出了说明:《How do I solve the Headers already sent warning problem?》。文章指出:要确保各个文件——尤其是经常被编辑的wp-config.php文件——以结尾,前后不能有其他字符。具体到上面的例子,很明显,提示信息说wp-config.php的第一行就开始了html输出,这有可能是第一行的

解决方法

WordPress中文论坛没有提供全文搜索的功能,只能搜索标题,所以我用Google搜索了一下Cannot modify header information site:wordpress.org.cn,好像碰到这个问题的人还真不少。目前大家用的WordPress主要是WordPress英文原版和几个WordPress中文版。我的中文包又不包含wp-config-sample.php文件,自然不关我的事;WordPress原版用的ASCII码,自然不包含BOM,也不会出这样的错误;xigang制作的WordPress中文版在WordPress中文论坛有下,我去下载了WordPress 2.0.4和2.0.3这两个,检查了一下,没有问题;点点游的WordPress 2.0.4中文版里,wp-config-sample.php文件用的是GB2312编码和DOS行尾符,GOD!不过这样也好,如果有人用记事本修改了这个文件,DOS行尾符不会造成编辑问题,GB2312编码不会造成BOM的问题


 cookie用法
关于删除cookie的说明开始-----

bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

要删除cookie需要确保它的失效期是在过去,才能触发浏览器的删除机制。

下面的例子说明了如何删除刚才设置的cookie:
//将过期时间设为一小时前
setcookie("TestCookie", "", time() - 3600);
setcookie("TestCookie", "", time() - 3600, "/~rasmus/", ".utoronto.ca", 1);
?>


-----关于删除cookie的说明结束-----

删除一个cookie的方法就是把这个cookie的有效期设置为当前时间以前,这也是几乎所有php程序员都会这么做。

后来一个初接触php的朋友告诉我,他在程序中本想把一个cookie的值设置为空,结果导致这个cookie直接被删除。我当时的第一反应是不相信,于是测试
了一下:
setcookie("testcookie", '');
print_r($_COOKIE);


结果果然是整个$_COOKIE数组都是空的,而非仅仅$_COOKIE['testcookie']为空。于是用winsock抓包,观察返回的http头,发现http头竟然是“Set-Cookie: testcookie=deleted; expires=Mon, 18-Jun-2007 02:42:33 GMT”,这说明“setcookie("testcookie", '');”的的确确是将testcookie这个cookie直接删除,而关于这种情况在php手册中完全没有说明。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles