本文實(shí)例講述了thinkPHP5框架設(shè)置404、403等http狀態(tài)頁(yè)面的方法。分享給大家供大家參考,具體如下:
要做到這一點(diǎn),首先要在你的配置文件將調(diào)試模式關(guān)閉(在開(kāi)發(fā)階段要打開(kāi)):
'app_debug' => false,
然后在配置文件 config.php 中配置404等頁(yè)面的模板路徑(APP_PATH指的是application路徑):
'http_exception_template' => [ // 定義404錯(cuò)誤的重定向頁(yè)面地址 404 => APP_PATH.'404.html', // 還可以定義其它的HTTP status 401 => APP_PATH.'401.html', 403 => APP_PATH.'404.html', ],
404頁(yè)面位于 application 目錄下,404.html部分代碼如下:
<img src="__INDEX__/img/404.png" width="818" height="595" style="display: block;margin: 0 auto;"> <div class="" style="font-size: 36px;margin: 0 auto;text-align: center;color: #323232;"> 您查找的頁(yè)面不存在,還有 <span id="dd" style="color:darkorange;font-weight: bold;">6</span> 秒,頁(yè)面將自動(dòng)跳轉(zhuǎn)首頁(yè)... </div> <!--倒計(jì)時(shí)--> <script type="text/javascript"> function run(){ var s = document.getElementById("dd"); if(s.innerHTML == 0){ window.location.href='/'; return false; } s.innerHTML = s.innerHTML * 1 - 1; } window.setInterval("run();", 1000); </script>
測(cè)試 controller
if (Request::instance()->isAjax()) { $data = input(); $info = []; $where = ''; switch ($data['msg']) { case '驗(yàn)證碼': $info = [ 'y' => '輸入正確', 'n' => '輸入錯(cuò)誤', ]; $where = session::get('admin_login_session') == md5($data['param']);break; } if ($where) { echo '{"info":"' . $data['msg'] . $info ['y'] . '","status":"y"}';//注意ValidForm返回格式(json) } else { echo '{"info":"' . $data['msg'] . $info ['n'] . '","status":"n"}';//注意ValidForm返回格式(json) } }else{ throw new \think\exception\HttpException(403, '~~~非法請(qǐng)求~~~'); }
404效果如下圖:
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。