PHP返回404状态码,由服务器处理

1. 通过header()方法来实现,

   最简单的方法,而且对php的版本没什么限制

   <?php

         header(‘HTTP/1.1 404 Not Found‘);

         header("status: 404 Not Found");

   ?>

   当服务器设置了404页面后,访问该php就会自动返回404状态及404页面。

 

2. 使用http_response_code()方法,

     如果你的php版本大于5.4的话,可以使用这个新增的方法

    <?php

        http_response_code(404);

    ?>

 

    注意:如果你的版本低于5.4,但是你又非得使用http_response_code(),下面是这个方法的代码

技术分享
 1 <?php
 2 
 3     if (!function_exists(‘http_response_code‘)) {
 4         function http_response_code($code = NULL) {
 5 
 6             if ($code !== NULL) {
 7 
 8                 switch ($code) {
 9                     case 100: $text = ‘Continue‘; break;
10                     case 101: $text = ‘Switching Protocols‘; break;
11                     case 200: $text = ‘OK‘; break;
12                     case 201: $text = ‘Created‘; break;
13                     case 202: $text = ‘Accepted‘; break;
14                     case 203: $text = ‘Non-Authoritative Information‘; break;
15                     case 204: $text = ‘No Content‘; break;
16                     case 205: $text = ‘Reset Content‘; break;
17                     case 206: $text = ‘Partial Content‘; break;
18                     case 300: $text = ‘Multiple Choices‘; break;
19                     case 301: $text = ‘Moved Permanently‘; break;
20                     case 302: $text = ‘Moved Temporarily‘; break;
21                     case 303: $text = ‘See Other‘; break;
22                     case 304: $text = ‘Not Modified‘; break;
23                     case 305: $text = ‘Use Proxy‘; break;
24                     case 400: $text = ‘Bad Request‘; break;
25                     case 401: $text = ‘Unauthorized‘; break;
26                     case 402: $text = ‘Payment Required‘; break;
27                     case 403: $text = ‘Forbidden‘; break;
28                     case 404: $text = ‘Not Found‘; break;
29                     case 405: $text = ‘Method Not Allowed‘; break;
30                     case 406: $text = ‘Not Acceptable‘; break;
31                     case 407: $text = ‘Proxy Authentication Required‘; break;
32                     case 408: $text = ‘Request Time-out‘; break;
33                     case 409: $text = ‘Conflict‘; break;
34                     case 410: $text = ‘Gone‘; break;
35                     case 411: $text = ‘Length Required‘; break;
36                     case 412: $text = ‘Precondition Failed‘; break;
37                     case 413: $text = ‘Request Entity Too Large‘; break;
38                     case 414: $text = ‘Request-URI Too Large‘; break;
39                     case 415: $text = ‘Unsupported Media Type‘; break;
40                     case 500: $text = ‘Internal Server Error‘; break;
41                     case 501: $text = ‘Not Implemented‘; break;
42                     case 502: $text = ‘Bad Gateway‘; break;
43                     case 503: $text = ‘Service Unavailable‘; break;
44                     case 504: $text = ‘Gateway Time-out‘; break;
45                     case 505: $text = ‘HTTP Version not supported‘; break;
46                     default:
47                         exit(‘Unknown http status code "‘ . htmlentities($code) . ‘"‘);
48                     break;
49                 }
50 
51                 $protocol = (isset($_SERVER[‘SERVER_PROTOCOL‘]) ? $_SERVER[‘SERVER_PROTOCOL‘] : ‘HTTP/1.0‘);
52 
53                 header($protocol . ‘ ‘ . $code . ‘ ‘ . $text);
54 
55                 $GLOBALS[‘http_response_code‘] = $code;
56 
57             } else {
58 
59                 $code = (isset($GLOBALS[‘http_response_code‘]) ? $GLOBALS[‘http_response_code‘] : 200);
60 
61             }
62 
63             return $code;
64 
65         }
66     }
67 
68 ?>
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。