php header 404跳转错误页面实例
404页面是一种友好的错误提示页面,在php中我们可以利用php,apache,hatcess三种方法来实现向浏览器发送404错误页面状态,下面我来介绍其它的php header函数实现404错误页面的具体方法.
先来了解header函数
header() 函数向客户端发送原始的 HTTP 报头,header(string,replace,http_response_code)
php header 404写法,代码如下:
header("HTTP/1.1 404 Not Found");exit;
如果以上代码不凑效,可以试试以下代码:
header("Status: 404 Not Found");
上面只是404状态页面很难看,如果我们要验证需要如下操作:
@header("http/1.1 404 not found"); @header("status: 404 not found"); include("404.html");//跳转到某一个页面,推荐使用这种方法 exit();
这样不但有错误提示信息同时还会有一个漂亮的页面,我在实例应用中的一个实例,代码如下:
<?php function getref() { $url = @$_SERVER['HTTP_REFERER']; if (!emptyempty($url)) { if (!strstr($url, 'phprm.com') && !strstr($url, 'phprm.com')) { @header("http/1.1 404 not found"); @header("status: 404 not found"); include ("404.html"); //跳转到某一个页面,推荐使用这种方法 exit(); } } else { @header("http/1.1 404 not found"); @header("status: 404 not found"); include ("404.html"); //跳转到某一个页面,推荐使用这种方法 exit(); } } ?>
如果讲到apache那就简单多了,ErrorDocument 404 /404.html
注意了,如果你写好之后一定要利用专题404错误页面工具进行测试,否则可能返回200就不好了,一定要看到是404状态才行.
本文地址:http://www.phprm.com/develop/fs3835.html
转载随意,但请附上文章地址:-)