Home >Backend Development >PHP Tutorial >About the method of setting http status pages such as 404 and 403 in thinkPHP5 framework

About the method of setting http status pages such as 404 and 403 in thinkPHP5 framework

不言
不言Original
2018-06-19 15:56:132853browse

This article mainly introduces the method of setting 404, 403 and other http status pages in the thinkPHP5 framework. It analyzes the related configuration of the thinkPHP5 framework setting the 404 page, the view display page and the related operation skills of the controller call in the form of examples. Friends in need. You can refer to the following example

This article describes the method of setting 404, 403 and other http status pages in the thinkPHP5 framework. Share it with everyone for your reference, the details are as follows:

To do this, you must first turn off the debugging mode in your configuration file (it needs to be turned on during the development stage):

'app_debug' => false,

Then configure the template path for 404 and other pages in the configuration file config.php (APP_PATH refers to the application path):

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

The 404 page is located in the application directory. The 404.html part of the code is as follows:

<img src="__INDEX__/img/404.png" width="818" height="595" style="display: block;margin: 0 auto;">
<p class="" style="font-size: 36px;margin: 0 auto;text-align: center;color: #323232;">
  您查找的页面不存在,还有
  <span id="dd" style="color:darkorange;font-weight: bold;">6</span>
  秒,页面将自动跳转首页...
</p>
<!--倒计时-->
<script type="text/javascript">
function run(){
  var s = document.getElementById("dd");
  if(s.innerHTML == 0){
    window.location.href=&#39;/&#39;;
    return false;
  }
  s.innerHTML = s.innerHTML * 1 - 1;
}
window.setInterval("run();", 1000);
</script>

Test controller

if (Request::instance()->isAjax()) {
  $data = input();
  $info = [];
  $where = &#39;&#39;;
  switch ($data[&#39;msg&#39;]) {
  case &#39;验证码&#39;:
    $info = [
    &#39;y&#39; => &#39;输入正确&#39;,
    &#39;n&#39; => &#39;输入错误&#39;,
    ];
  $where = session::get(&#39;admin_login_session&#39;) == md5($data[&#39;param&#39;]);break;
 }
 if ($where) {
  echo &#39;{"info":"&#39; . $data[&#39;msg&#39;] . $info [&#39;y&#39;] . &#39;","status":"y"}&#39;;//注意ValidForm返回格式(json)
 } else {
  echo &#39;{"info":"&#39; . $data[&#39;msg&#39;] . $info [&#39;n&#39;] . &#39;","status":"n"}&#39;;//注意ValidForm返回格式(json)
 }
}else{
 throw new \think\exception\HttpException(403, &#39;~~~非法请求~~~&#39;);
}

404 The effect is as follows:

The above is the entire content of this article. I hope it will be helpful to everyone’s study. More For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About ThinkPHP’s method of using UTFWry address library for IP positioning

The above is the detailed content of About the method of setting http status pages such as 404 and 403 in thinkPHP5 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn