php 用 header指定返回码的函数

2014年05月03日 13:12 by:PHP手册

导读: php用header指定网页当前的返回码是非常重要的, 返回码其实是给"搜索引擎"看的, 比如删除了一个网页, 那么查询的时候就要返回404, 分类目录还没有添加内容这时候就要返回503了, 不要以为这些返回码无所谓, 这关系到百度的收录情况. 如果不指定返回码可能导致降权或者不收录.

记录一下, 最常用的函数, 各种返回码, 方便调用. 不要觉得烦, 网页不同的需要要及时返回相应的代码, 比如删除了一个网页, 那么查询的时候就要返回404, 分类目录还没有添加内容这时候就要返回503了, 不要以为这些返回码无所谓, 这关系到百度的收录情况. 如果不指定返回码可能导致降权或者不收录. 

// 使用方法->比如指定404返回码用: httpstatus(404);
  1. function httpstatus($num){//网页返回码
  2. static $http = array (
  3. 100 => "HTTP/1.1 100 Continue",
  4. 101 => "HTTP/1.1 101 Switching Protocols",
  5. 200 => "HTTP/1.1 200 OK",
  6. 201 => "HTTP/1.1 201 Created",
  7. 202 => "HTTP/1.1 202 Accepted",
  8. 203 => "HTTP/1.1 203 Non-Authoritative Information",
  9. 204 => "HTTP/1.1 204 No Content",
  10. 205 => "HTTP/1.1 205 Reset Content",
  11. 206 => "HTTP/1.1 206 Partial Content",
  12. 300 => "HTTP/1.1 300 Multiple Choices",
  13. 301 => "HTTP/1.1 301 Moved Permanently",
  14. 302 => "HTTP/1.1 302 Found",
  15. 303 => "HTTP/1.1 303 See Other",
  16. 304 => "HTTP/1.1 304 Not Modified",
  17. 305 => "HTTP/1.1 305 Use Proxy",
  18. 307 => "HTTP/1.1 307 Temporary Redirect",
  19. 400 => "HTTP/1.1 400 Bad Request",
  20. 401 => "HTTP/1.1 401 Unauthorized",
  21. 402 => "HTTP/1.1 402 Payment Required",
  22. 403 => "HTTP/1.1 403 Forbidden",
  23. 404 => "HTTP/1.1 404 Not Found",
  24. 405 => "HTTP/1.1 405 Method Not Allowed",
  25. 406 => "HTTP/1.1 406 Not Acceptable",
  26. 407 => "HTTP/1.1 407 Proxy Authentication Required",
  27. 408 => "HTTP/1.1 408 Request Time-out",
  28. 409 => "HTTP/1.1 409 Conflict",
  29. 410 => "HTTP/1.1 410 Gone",
  30. 411 => "HTTP/1.1 411 Length Required",
  31. 412 => "HTTP/1.1 412 Precondition Failed",
  32. 413 => "HTTP/1.1 413 Request Entity Too Large",
  33. 414 => "HTTP/1.1 414 Request-URI Too Large",
  34. 415 => "HTTP/1.1 415 Unsupported Media Type",
  35. 416 => "HTTP/1.1 416 Requested range not satisfiable",
  36. 417 => "HTTP/1.1 417 Expectation Failed",
  37. 500 => "HTTP/1.1 500 Internal Server Error",
  38. 501 => "HTTP/1.1 501 Not Implemented",
  39. 502 => "HTTP/1.1 502 Bad Gateway",
  40. 503 => "HTTP/1.1 503 Service Unavailable",
  41. 504 => "HTTP/1.1 504 Gateway Time-out"
  42. );
  43. header($http[$num]);
  44. }