Home Backend Development PHP Tutorial Warning Cannot send session cookie - headers already sent.有关问题的解决(PHP的UTF-8 BOM引

Warning Cannot send session cookie - headers already sent.有关问题的解决(PHP的UTF-8 BOM引

Jun 13, 2016 pm 01:04 PM
nbsp php session start

Warning Cannot send session cookie - headers already sent...问题的解决(PHP的UTF-8 BOM引
习惯了用edit plus进行php编程,所以有时会出现一些不为人知的错误,很麻烦;
近日,在开发项目时,某些页面总是出现以下问题:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:\web\Apache2\htdocs\index.php:1) in E:\web\Apache2\htdocs\functions\sessions.php on line 67

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:\web\Apache2\htdocs\index.php:1) in E:\web\Apache2\htdocs\functions\sessions.php on line 67
经过详细搜索,得到以下原因:
我的edit plus中设置了默认的编码为utf-8,且UTF_8签名为:总是添加签名;
于是尝试以下操作:
在edit plus 的工具->参数->文件->UTF_8签名一项中,更改选项"总是添加签名"为"总是移除签名",然后打开index.php文件,并重新另存为,重新运行脚本,终于可以正常了;

另外,在网上找到了两篇比较有参考价值的文章,希望有人碰到此种情况时可以完美解决!


一、一个UTF-8 BOM引起的PHP的诡异问题

//---a.php
header("Content-Type: image/BMP");
session_start();
................
?>
将a.php保存为utf-8格式,结果用浏览器访问这个php文件,就会出现如下错误:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started

at ×××.php:1) in ×××on line 2

这个问题很常见,多数是因为在session_start之前有输出了!对于老鸟来说,这个错误基本上不会发生,但是如果你是用DW或是editplus等编

辑器写代码的,连高手也有可能发生这个错误!

如上面的提示:在第×××文件的第1行,×××文件的第2行,随你看,这两处是不会有任何输出语句的,很奇怪还是会出错,为什么呢
原来:

Unicode 签名 (BOM) 可在文档中包括字节顺序标记 (BOM)。BOM 是位于文本文件开头的 2 到 4 个字节,可将文件标识为 Unicode,如果是这

样,还标识后面字节的字节顺序。由于 UTF-8 没有字节顺序,因此可以选择添加 UTF-8 BOM。对于 UTF-16 和 UTF-32,这是必需的。
看见没有!如果选了这个选项,就会在页面的最前面输出2到4个字节!

而 session_start() 要求之前没有任何输出给客户端浏览器


二、另外还有一个地方可能会出错,例如:
/--a.php--
?>
空行
空行

如果你包含a.php之后再来也会有这个问题,通常的建议是经常被包含的文件末尾不要有?>

又如:
在调用Session_Start()之前不能有任何输出.例如下面是错
误的.
==========================================
1行
2行3行 Session_Start();//之前在第一行已经有输出
4行.....
5行?>
==========================================

已经经过试验,事实确实是如此诡异。

三、

session_start()
set_cookie()
header()
前面都加上@应该可以抑制这个警告。


四、
在editplus编辑器中,如果先把utf-8的a.php文件转换为gb2312或是其他,然后再转换为utf-8这样就可以成功访问了,也就是说文件开头的

BOM被去掉了,这时候的UTF-8 是无BOM类型的了


PHP-关于utf-8编码问题引起的session_start()错误


大中小
采用默认的gb2312编码时,兼容Ansi编码,文件头部无任何附加信息,此时session_start()可以正常工作。
采用utf编码时,大部分编辑器都会在在文件头部附加一个BOM块,我的EditPlus附加的是FF FE,用16进制编辑器
可以很清楚的看到。这样,当调用session_start()时,实际上已经向浏览器输出两个字节,只不过是不可见字符浏
览器中出现如下警告:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at ......................

解决方法:
1、手动去掉BOM块,可以在16进制编辑器如UltraEdit中编辑,或者采用编辑器自带的功能,好的编辑器一般提供选择是否去除BOM块。
2、自己编写脚本更正,这要针对不同的编辑器,BOM头定义:
UTF-8                          EF BB BF
UTF-16 Big Endian              FE FF
UTF-16 Little Endian           FF FE
UTF-32 Big Endian 00 00        FE FF
UTF-32 Little Endian           FF FE 00 00

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)

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles