zoukankan      html  css  js  c++  java
  • PHP发送返回404状态码

    1. 默认的由Apache自动处理的404

        修改Aache的配置文件 httpd.conf 中的

        ErrorDocument 404 /404.html

        或者使用 .htaccess文件,同时有要把 httpd.conf中的deny都改成allow

        文件里也是写上面那句 ErrorDocument 404 /404.html 就可以了

    2. 人工强制返回404状态码

    1 function error_to_back_home(){
    2     /*@是抑制显示错误信息,有错也不显示*/
    3     @header("http/1.1 404 not found"); 
    4     @header("status: 404 not found"); 
    5     @header("location: /404.html"); //跳转到404页面
    6     exit();
    7 }
    View Code

        这个代码虽然能够实现我的要求,但是本人依然觉得很奇怪,

        难道不是只要我返回404状态码,Apache也做了相应的404配置,

        这样不会自动跳转到404页面的吗,非得要我手动跳转?期待高手解答一下。

        针对某些特殊情况,对上面的代码进一步优化:

    1 @header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");//不区分协议1.1 /1.0
    2 @header("Status: 404 Not Found");
    3 
    4 $_SERVER['REDIRECT_STATUS'] = 404;
    5 //If you don't know which web page is in use, use any page that doesn't exists
    6 $handle = curl_init('http://'. $_SERVER["HTTP_HOST"] .'/404missing.html');
    7 curl_exec($handle);
    8 exit();
    View Code

       

  • 相关阅读:
    THINKPHP3.2视频教程
    PHPCMS 学习
    PHPCMS 后台学习
    phpcms 模板学习
    二叉树的创建与遍历(链式存储)
    MySQL基础~~增、删、改、简单查
    队列的基本操作(链队列)
    MySQL基础~~表结构操作
    行编辑程序
    循环链表的基本操作
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/4497603.html
Copyright © 2011-2022 走看看