Home  >  Article  >  Backend Development  >  PHP跳转Header(“location:”)的注意事项

PHP跳转Header(“location:”)的注意事项

WBOY
WBOYOriginal
2016-06-20 13:05:152181browse

header(“Location: “) 作为php的转向语句 ,可以使当前页面跳转到其他页面。在使用中需要注意:

1、用法

header(“Location:http://www.scutephp.com/ ”)

2.header前应没有任何输出。

3.如果之前有输出:

(1)会出现类似如下报错:Warning: Cannot modify header information – headers already sent by  (output started at …….php:12) in …….php on line  N

(2)这时可以使用ob,它可以是在服务器端先存储有关输出,等待适当的时机再输出。如果不使用则为运行一句,输出一句,发现header语句就会报错。

具体的语句有: ob_start(); ob_end_clean();ob_flush();………

4、在header(“Location:http://www.scutepp.com/”)后要及时exit

否则他是会继续执行的,虽然在浏览器端你看不到相应的数据出现,但是如果你进行抓包分析的话,你就会看到下面的语句也是在执行的。而且被输送到了浏览器客户端,只不过是没有被浏览器执行为html而已(浏览器执行了header进行了转向操作)。

所以,标准的使用方法是 :

ob_start();

……..

if (…… ){

ob_end_clean();

header(“Location:http://www.yanfei.info/ ”);

exit;

else{

……….

ob_flush(); //可略

 


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