>  기사  >  백엔드 개발  >  PHP에서 오류 페이지를 사용자 정의하는 방법

PHP에서 오류 페이지를 사용자 정의하는 방법

王林
王林원래의
2021-10-13 16:36:222390검색

PHP에서 오류 페이지를 사용자 정의하는 방법: 1. 구성 파일에 오류 페이지 템플릿 정보를 추가합니다. 2. 구성 정보를 현재 모델의 /Conf/config.php에 넣습니다. 3. 해당 모델의 View 디렉터리에 만듭니다. 현재 모델 공용 폴더를 수정하고 그 안에 있는 오류 페이지를 사용자 정의하세요.

PHP에서 오류 페이지를 사용자 정의하는 방법

이 기사의 운영 환경: windows10 시스템, php 7&&thinkphp 5, thinkpad t480 컴퓨터.

ThinkPHP 경량 PHP 개발 프레임워크로서 풍부한 문서를 갖추고 있으며 다른 프레임워크보다 사용하기 쉽습니다. 따라서 여기서는 맞춤형 오류 페이지를 구현하기 위해 thinkphp를 선택했습니다.

ThinkPHP 자체는 자체 오류 페이지, 예외 페이지 및 기타 정보 프롬프트 페이지를 제공합니다. 예를 들어 다음 코드는 이러한 프롬프트를 표시합니다.

$this->error('验证码错误!');

내장 페이지가 아름답지 않기 때문에 이를 사용자 정의해야 합니다. 페이지, ThinkPHP는 프롬프트 페이지를 사용자 정의하는 기능을 제공합니다.

구성 파일에 다음 구성 정보를 추가합니다.

/* 错误页面模板 */
'TMPL_ACTION_ERROR'     =>  MODULE_PATH.'View/Public/error.html', // 默认错误跳转对应的模板文件
'TMPL_ACTION_SUCCESS'   =>  MODULE_PATH.'View/Public/success.html', // 默认成功跳转对应的模板文件
'TMPL_EXCEPTION_FILE'   =>  MODULE_PATH.'View/Public/exception.html',// 异常页面的模板文件

이 구성 정보를 현재 모델의 /Conf/config.php에 넣은 다음 현재 모델의 View 디렉터리에 Public 폴더를 생성하여 해당 폴더에 넣습니다. 그냥 error.html Success.html과 예외.html을 맞춤설정하세요.

다음은 간단한 오류 페이지 템플릿입니다:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>跳转提示</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
body{ background: #290C0C; font-family: &#39;微软雅黑&#39;; color: #fff; font-size: 16px; }
.system-message{ padding: 24px 48px; }
.system-message h1{ font-size: 80px; font-weight: normal; line-height: 120px; margin-bottom: 12px }
.system-message .jump{ padding-top: 10px;margin-bottom:20px}
.system-message .jump a{ color: #333;}
.system-message .success,.system-message .error{ line-height: 1.8em; font-size: 36px }
.system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px; display:none}
#wait {
    font-size:46px;
}
#btn-stop,#href{
    display: inline-block;
    margin-right: 10px;
    font-size: 16px;
    line-height: 18px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    border: 0 none;
    background-color: #8B0000;
    padding: 10px 20px;
    color: #fff;
    font-weight: bold;
    border-color: transparent;
    text-decoration:none;
}
  
#btn-stop:hover,#href:hover{
    background-color: #ff0000;
}
</style>
</head>
<body>
<div class="system-message">
<h1>提示信息!</h1>
<?php if(isset($message)) {?>
<p class="error"><?php echo($message); ?></p>
<?php }else{?>
<p class="error"><?php echo($error); ?></p>
<?php }?>
<p class="detail"></p>
<p class="jump">
<b id="wait"><?php echo($waitSecond); ?></b> 秒后页面将自动跳转
</p>
<div>
    <a id="href" id="btn-now" href="<?php echo($jumpUrl); ?>">立即跳转</a> 
    <button id="btn-stop" type="button" onclick="stop()">停止跳转</button> 
    <a id="href" id="btn-now" href="<?php echo(U(&#39;Public/logout&#39;)); ?>">重新登录</a> 
</div>
</div>
<script type="text/javascript">
(function(){
 var wait = document.getElementById(&#39;wait&#39;),href = document.getElementById(&#39;href&#39;).href;
 var interval = setInterval(function(){
       var time = --wait.innerHTML;
       if(time <= 0) {
           location.href = href;
           clearInterval(interval);
       };
     }, 1000);
  window.stop = function (){
         console.log(111);
            clearInterval(interval);
 }
 })();
</script>
</body>
</html>

효과:

PHP에서 오류 페이지를 사용자 정의하는 방법

권장 학습: php training

위 내용은 PHP에서 오류 페이지를 사용자 정의하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.