Home Backend Development PHP Tutorial php无法保存cookies有关问题解决

php无法保存cookies有关问题解决

Jun 13, 2016 pm 01:06 PM
config header php setcookie wordpress

php无法保存cookies问题解决

今天弄php程序,突然发现一个问题,就是cookies无法保存,且不发生任何错误,经过一番查找发现问题如下:

因为程序在写Cookies是用的是?@setcookie ( "url", "http://www.my400800.cn ", time()+3600*24, "/");

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 files\easyphp1-8\www\wp-config.php:1) in c:\program files\easyphp1-8\www\wp-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输出,这有可能是第一行的本编辑了wp-config.php文件并保存成了UTF-8编码的文档,从而因为BOM的三个字符的输出造成了header命令执行出错 。

?

解决方法

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的问题,呼。

唉,如果你要用WordPress架Blog,还是扔掉记事本,装个UltraEdit或者EditPlus吧!

?

?

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

What are the plugins for wordpress blocking ip What are the plugins for wordpress blocking ip Apr 20, 2025 am 08:27 AM

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to write a header of a wordpress How to write a header of a wordpress Apr 20, 2025 pm 12:09 PM

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to import wordpress templates How to import wordpress templates Apr 20, 2025 am 10:18 AM

WordPress templates quickly create professional websites. The steps to import a template include: select and download the template. Log in to the WordPress dashboard. Select Theme from the Appearance menu. Click "Add New Theme". Click "Upload topic" and select the downloaded template .zip file. Click "Install Now". Click the "Activate" button. Customize the templates through the Customize menu.

See all articles