PHP笔记-所有错误统一输出404页面(详细错误日志输出,提高安全性)
生活随笔
收集整理的這篇文章主要介紹了
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页面(详细错误日志输出,提高安全性)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python色标_Python: 气象绘
- 下一篇: python __set__ __get