php header()函数跳转无效解决

WBOY
Freigeben: 2016-06-20 13:03:00
Original
1813 Leute haben es durchsucht

在转换编码的时候,有一个页面需要在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();
Nach dem Login kopieren

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>"; ?>
Nach dem Login kopieren

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();

?>
Nach dem Login kopieren

就像上面的代码那样,这种方法在生成页面的时候缓存,这样就允许在输出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执行效率,这取决于服务器的性能和代码的复杂度。


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!