search
HomeBackend DevelopmentPHP Tutorial php无法保存cookies有关问题解决

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
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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software