教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 直接用php的header发送404错误页面的方法实例

直接用php的header发送404错误页面的方法实例

发布时间:2016-10-16   编辑:jiaochengji.com
教程集为您提供直接用php的header发送404错误页面的方法实例等资源,欢迎您收藏本站,我们将为您提供最新的直接用php的header发送404错误页面的方法实例资源
在开发网站的时候,有时我们为了友好的提示用户输入的网址不存在,用设置一个 404 错误页面。设置 404 还有一个好处就是防止恶意用户猜测你的url参数。本文我们来讲讲用php及apache设置404错误页面的方法。

在php程序中,我们也经常需要考虑如何用php实现页面404跳转的写法,下面作者将如何用php进行404转向的写法和大家分享一下!

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3753')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3753>// 直接输出页面错误信息
@header("http/1.1 404 not found");
@header("status: 404 not found");
echo 'echo 404';
exit();



或者:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3250')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3250>// 跳转到错误页面,推荐使用这种方法
// 注意include文件的路径不能是http网络路径
@header("http/1.1 404 not found");
@header("status: 404 not found");
include("/404.html");
exit();




我们也可以直接在 apache 设置 404 错误页面,具体定义方法如下:
 
在网站的根目录下新建一个 .htaccess 文件,然后在该文件中加入如下内容即可:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4334')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4334>ErrorDocument 404 http://www.phpernote.com/404.html




查看404是否设置成功可以利用 firefox 的 firebug 插件来查看,具体查看方法如下:

在已经安装firebug的情况下,用firefox浏览器打开要检查404状态码的页面,点击右下角小虫的图标,启动"网络"标签,依次打开"网络–所有/html–headers";如果一切正常,你就可以看到包括headers、响应在内的各项页面参数了。

您可能感兴趣的文章:
直接用php的header发送404错误页面的方法实例
php header 404跳转错误页面实例
php中header函数的用法举例详解
404 Not Found代码是什么意思 404错误是什么意思
php header是什么意思
PHP header()函数(301、404等错误设置)的用法
php header 使用详解
php header函数用法详解举例(2)
PHP header函数用法举例
php header()函数的简单例子

[关闭]
~ ~