在转换编码的时候,有一个页面需要在head中申明utf-8的编码,但是这与程序中的一处header产生了冲突。google了一下,找到几种解决方法,翻译整理一下:
If you got this message: "Warning: Cannot modify header information - headers already sent by ...."
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ...."
Few notes based on the following user posts:
有以下几种解决方法:
1. Blank lines (空白行):
检查有 后面没有空白行,特别是include或者require的文件。不少问题是这些空白行导致的。
2. Use exit statement (用exit来解决):
Use exit after header statement seems to help some people
在header后加上exit();
header ("Location: xxx"); exit();
3. PHP has this annoying problem, if your HTML goes before any PHP code or any header modification before redirecting to certain page, it ll said "Warning: Cannot modify header information - headers already sent by ...." Basically anytime you output to browser, the header is set and cannot be modified. So two ways to get around the problem:
3a. Use Javascrīpt (用Javascrīpt来解决):
<? echo "<scrīpt> self.location( file.php );</scrīpt>"; ?>
Since it s a scrīpt, it won t modify the header until execution of Javascrīpt.
可以用Javascrīpt来代替header。另外需要注意,采用这种方法需要浏览器支持Javascrīpt.
3b. Use output buffering (用输出缓存来解决):---本人用这种方法解决的!已证实可以!
<?php ob_start(); ?> ... HTML codes ... <?php ... PHP codes ... header ("Location: ...."); ob_end_flush(); ?>
就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出head之后再输出header了。本站的许愿板就是采用这种方法解决的header问题。
4.set output_buffering = On in php.ini (开启php.ini中的output_buffering )
set output_buffering = On will enable output buffering for all files. But this method may slow down your php output. The performance of this method depends on which Web server you re working with, and what kind of scrīpts you re using.
这种方法和3b的方法理论上是一样的。但是这种方法开启了所有php程序的输出缓存,这样做可能影响php执行效率,这取决于服务器的性能和代码的复杂度。

本文讨论了PHP中的crypt()和password_hash()之间的差异,以进行密码哈希,重点介绍其实施,安全性和对现代Web应用程序的适用性。

文章讨论了通过输入验证,输出编码以及使用OWASP ESAPI和HTML净化器之类的工具来防止PHP中的跨站点脚本(XSS)。

自动加载PHP会在需要时自动加载类文件,从而通过减少内存使用和增强代码组织来提高性能。最佳实践包括使用PSR-4和有效组织代码。

本文讨论了在PHP中管理文件上传大小的管理,重点是2MB的默认限制以及如何通过修改PHP.INI设置来增加它。

本文讨论了PHP 7.1中引入的PHP中的无效类型,允许变量或参数为指定类型或NULL。它突出显示了诸如提高可读性,类型安全性和明确意图的好处,并解释了如何声明

本文讨论了unset()和unlink()功能在编程中的差异,重点关注其目的和用例。 unset()从内存中删除变量,而unlink()从文件系统中删除文件。两者都对效率至关重要


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

记事本++7.3.1
好用且免费的代码编辑器