日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP笔记-所有错误统一输出404页面(详细错误日志输出,提高安全性)

發布時間:2025/3/15 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP笔记-所有错误统一输出404页面(详细错误日志输出,提高安全性) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里我用的是自定義MVC,所以統一錯誤頁面很簡單,自定義MVC框架在這篇博文

PHP筆記-自定義MVC框架_IT1995的博客-CSDN博客

當輸入任意不存在的頁面時:

這里在

private static function setDispatch(){try{$controller = "\\" . P . "\\controller\\" . C . "Controller";$action = A;$object = new $controller;$object->$action();}catch (\Throwable $e){//進入 404頁面//echo "Message: " . $e->__toString();//這里開始 輸出到日志//這里結束 輸出到日志self::load404Page();$controller = new PageNotFindController();$controller->index();}}

這里使用__toString()可以輸出字符串,然后將其輸入到服務器日志里面,就能記錄日志,不被心懷不軌的人發現了。

這里有一個要注意的地方就是Throwable

/*** Throwable is the base interface for any object that can be thrown via a throw statement in PHP 7,* including Error and Exception.* @link https://php.net/manual/en/class.throwable.php* @since 7.0*/ interface Throwable extends Stringable

這里可以知道,他可以捕獲錯誤和異常,是php7中新引用的。

其中load404Page()函數如下:

private static function load404Page(){$controllerFile = APP_PATH . "user/controller/PageNotFindController.php";if(file_exists($controllerFile)){include $controllerFile;}}

PageNotFindController.php

<?phpnamespace user\controller;use core\Controller;class PageNotFindController {protected $smarty;public function __construct(){if(!class_exists("Smarty")){include VENDOR_PATH . "smarty/Smarty.class.php";}$this->smarty = new \Smarty();$this->smarty->template_dir = APP_PATH . "user/view/";$this->smarty->compile_dir = RESOURCES_PATH . "views";}public function index(){$this->smarty->display("page404.html");} }

這里僅僅能實現功能,提供一個思路。

總結

以上是生活随笔為你收集整理的PHP笔记-所有错误统一输出404页面(详细错误日志输出,提高安全性)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。