thinkPHP5框架设置404、403等http状态页面的方法

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了thinkPHP5框架设置404、403等http状态页面的方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了thinkPHP5框架设置404、403等http状态页面的方法。分享给大家供大家参考,具体如下:

做到一点,首先要在你的配置文件将调试模式关闭(在开发阶段要打开):

'app_debug' => false,

然后在配置文件 config.php 中配置404等页面的模板路径(APP_PATH指的是application路径):

'http_exception_template'  => [
    // 定义404错误的重定向页面地址
    404 => APP_PATH.'404.htML',
    // 还可以定义其它的HTTP status
    401 => APP_PATH.'401.html',
    403 => APP_PATH.'404.html',
  ],

404页面位于 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;">
  您查找的页面不存在,还有
  <span id="dd" style="color:darkorange;font-weight: bold;">6</span>
  秒,页面将自动跳转首页...
</div>
<!--倒计时-->
<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>

测试&nbsp; controller

if (Request::instance()->isAjax()) {
  $data = input();
  $info = [];
  $where = '';
  swITch ($data['msg']) {
  case '验证码':
    $info = [
    'y' => '输入正确',
    'n' => '输入错误',
    ];
  $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, '~~~非法请求~~~');
}

404效果如下图:

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

脚本宝典总结

以上是脚本宝典为你收集整理的thinkPHP5框架设置404、403等http状态页面的方法全部内容,希望文章能够帮你解决thinkPHP5框架设置404、403等http状态页面的方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。