首页 > 开发 > PHP > 正文

php header 404跳转错误页面实例

2024-05-04 21:58:09
字体:
来源:转载
供稿:网友

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状态页面很难看,如果我们要验证需要如下操作:

  1. @header("http/1.1 404 not found"); 
  2. @header("status: 404 not found"); 
  3. include("404.html");//跳转到某一个页面,推荐使用这种方法 
  4. exit(); 

这样不但有错误提示信息同时还会有一个漂亮的页面,我在实例应用中的一个实例,代码如下:

  1. function getref() 
  2.  $url = @$_SERVER['HTTP_REFERER']; 
  3.  if( !emptyempty$url ) ) 
  4.  { 
  5.   if( !strstr($url ,'Vevb.com' ) && !strstr($url,'Vevb.com')) 
  6.   { 
  7.    @header("http/1.1 404 not found"); 
  8.    @header("status: 404 not found"); 
  9.    include("404.html");//跳转到某一个页面,推荐使用这种方法 
  10.    exit(); 
  11.   } 
  12.  } 
  13.  else 
  14.  { 
  15.   @header("http/1.1 404 not found"); 
  16.   @header("status: 404 not found"); 
  17.   include("404.html");//跳转到某一个页面,推荐使用这种方法 
  18.   exit(); 
  19.  } 

如果讲到apache那就简单多了,ErrorDocument 404 /404.html

注意了,如果你写好之后一定要利用专题404错误页面工具进行测试,否则可能返回200就不好了,一定要看到是404状态才行.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表