Home Backend Development PHP Tutorial php header()函数跳转无效解决

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

Jun 20, 2016 pm 01:03 PM
Page jump

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

1

2

3

header ("Location: xxx");

 

exit();

Copy after login

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来解决):

1

<? echo "<scrīpt> self.location( file.php );</scrīpt>"; ?>

Copy after login

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 (用输出缓存来解决):---本人用这种方法解决的!已证实可以!

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php ob_start(); ?>

 

... HTML codes ...

 

<?php

 

... PHP codes ...

 

header ("Location: ....");

 

ob_end_flush();

 

?>

Copy after login

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


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 Article

Hot tools Tags

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)

Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions Nov 18, 2023 pm 05:08 PM

Detailed explanation of PHP page jump function: page jump skills of header, location, redirect and other functions

Use uniapp to achieve page jump animation effect Use uniapp to achieve page jump animation effect Nov 21, 2023 pm 02:15 PM

Use uniapp to achieve page jump animation effect

PHP code example: How to use POST to pass parameters and implement page jumps PHP code example: How to use POST to pass parameters and implement page jumps Mar 07, 2024 pm 01:45 PM

PHP code example: How to use POST to pass parameters and implement page jumps

UniApp error: Unable to find a solution for 'xxx' page jump UniApp error: Unable to find a solution for 'xxx' page jump Nov 25, 2023 am 09:53 AM

UniApp error: Unable to find a solution for 'xxx' page jump

How to implement page jump and navigation in uniapp How to implement page jump and navigation in uniapp Oct 20, 2023 pm 02:07 PM

How to implement page jump and navigation in uniapp

Summary of JavaScript routing and page jump experience in front-end development Summary of JavaScript routing and page jump experience in front-end development Nov 02, 2023 am 10:15 AM

Summary of JavaScript routing and page jump experience in front-end development

How to use routing to implement page jump in Vue? How to use routing to implement page jump in Vue? Jul 21, 2023 am 08:33 AM

How to use routing to implement page jump in Vue?

Detailed explanation of page jump operation steps in Golang Detailed explanation of page jump operation steps in Golang Mar 05, 2024 pm 05:33 PM

Detailed explanation of page jump operation steps in Golang

See all articles