教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中header函数后是否应该有exit

php中header函数后是否应该有exit

发布时间:2016-10-16   编辑:jiaochengji.com
教程集为您提供php中header函数后是否应该有exit等资源,欢迎您收藏本站,我们将为您提供最新的php中header函数后是否应该有exit资源
php header后是否需要书写exit以终止程序,以前一直没有太在意,今天遇到个bug,才发现这还是个问题,特意写了段代码测试了一下,代码如下:


通常有一下三种:

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy1747')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy1747" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

Location: xxxx:yyyy/zzzz

Content-Type: xxxx/yyyy

Status: nnn xxxxxx

</td> </tr> </tbody> </table>

常用实例

1.实现重定向(状态码302)

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy9360')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy9360" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

<?php
header(”Location: http://www.jiaochengji.com”);
exit;
?>

</td> </tr> </tbody> </table>

例子

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy1673')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy1673" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2"><?php
header("Location:http://www.jiaochengji.com/");
//exit();
file_put_contents($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."n.txt",'php程序员教程网',LOCK_EX);</td> </tr> </tbody> </table>

果证明php文件运行过程中如果碰到有header("Location:某个页面URL");这种语句,会直接跳转到另一个页面,但是这里的跳转并没有影响当前php文件的继续执行。也就是是说php header跳转之后,当前页面的脚本会继续执行。如果需要终止脚本运行,需要加exit()或者die()。


但对于有些用法就不需要了

404代码

<table style="background: #fb7" border="0" cellspacing="1" cellpadding="1" width="620" align="center"> <tbody> <tr> <td bgcolor="#ffe7ce" height="27" width="464"> 代码如下</td> <td style="cursor: pointer" bgcolor="#ffe7ce" width="109" align="center" onclick="doCopy('copy1688')">复制代码</td> </tr> <tr> <td style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px" id="copy1688" class="copyclass" bgcolor="#ffffff" valign="top" colspan="2">

<?php
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>

301

<?
Header( "HTTP/1.1 301 Moved Permanently" ) ;
Header( "Location: www.jiaochengji.com" );
?>

</td> </tr> </tbody> </table>

 

您可能感兴趣的文章:
php header()函数详细介绍与实例
自动跳转url的php代码是什么
php中header函数的用法举例详解
PHP如何使用header()函数刷新页面?
PHP页面跳转(URL地址跳转)实现代码
php header函数用法详解举例(2)
PHP header函数用法举例
PHP页面跳转的三种方法
PHP页面跳转的几种方式
php中header函数后是否应该有exit

[关闭]
~ ~