為 XOOPS 佈景建立錯誤頁面

XOOPS

  諸如 404、410 ...錯誤碼所顯示的錯誤訊息頁面是很常見的事,本篇分享如何將錯誤頁面與佈景相融合,將錯誤訊息顯示於佈景下之模組內容區域裡。

以下是以 XOOPS 2.56 版本作示例:

建立名為 error.php 新檔案,置於網站目錄之下,就是 XOOPS/error.php 此路徑。

開啟 error.php,寫入如下程式碼:

<?php
include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mainfile.php';

  if (isset($_GET['error']))  {
    $error = $_GET['error'];
  }

  $title = '';
  $text = '';
  switch($error)
  {
    case 400:
      $title = _XO_ER_TITLE_400;
      $text = _XO_ER_DESC_400;
       break;
    case 401:
      $title = _XO_ER_TITLE_401;
      $text = _XO_ER_DESC_401;
    break;
    case 404:
      $title = _XO_ER_TITLE_404;
      $text = _XO_ER_DESC_404;
    break;
    case 406:
      $title = _XO_ER_TITLE_406;
      $text = _XO_ER_DESC_406;
    break;
    case 410:
      $title = _XO_ER_TITLE_410;
      $text = _XO_ER_DESC_410;
    break;
    default:
    	header('Location:'. XOOPS_URL . '/index.php');
    break;
  }
    $xoopsOption['template_main'] = "db:system_error.html";
    include $GLOBALS['xoops']->path('header.php');
    $xoopsTpl->assign('err_code',$error);
    $xoopsTpl->assign('err_title',$title);
    $xoopsTpl->assign('err_text',$text);
    include $GLOBALS['xoops']->path('footer.php');
?>

開啟 XOOPS/language/網站所用語言/error.php,在最後一行 define 後面新增:

define('_XO_ER_TITLE_400','不正確的要求');
define('_XO_ER_TITLE_401','尚未授權');
define('_XO_ER_TITLE_404','不存在');
define('_XO_ER_TITLE_406','不接受');
define('_XO_ER_TITLE_410','已移除');
define('_XO_ER_DESC_400','無法解讀要求的語法');
define('_XO_ER_DESC_401','需要驗證');
define('_XO_ER_DESC_404','找不到要求的網頁或檔案');
define('_XO_ER_DESC_406','無法以所要求的內容特性回應要求的網頁');
define('_XO_ER_DESC_410','要求的資源已永久移除');

  XOOPS 2.56 tchinese_utf8 者請注意:其下的 error.php 語言檔編碼有錯誤,在儲存檔案前請自行轉換為無 BOM utf8 編碼格式,否則儲存後會出現亂碼。

建立錯誤頁樣板檔:

  在 XOOPS/modules/system/templates/之下建立 system_error.html 新檔作為預設樣板檔,於檔中加入如下代碼:

<h2><{$err_code}> - <{$err_title}></h2>
<p><{$err_text}></p>

  若要變出個性化錯誤訊息內容,可複製 system_error.html 到 XOOPS/themes/所用佈景主題/modules/system/之下,然後就可將您的創意發揮出來。

開啟 XOOPS/modules/system/xoops_version.php,在 // Templates 下的最後一行新增

// Templates
......
......
......
$modversion['templates'][] = array( 'file' => 'system_error.html', 'description' => '' );

// Admin Templates

到後台模組管理,更新 system 模組。

編輯 XOOPS/.htaccess(如沒有 .htaccess 請先建立新檔),在 .htaccess 裡加入:

<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 400 /error.php?error=400
ErrorDocument 401 /error.php?error=401
ErrorDocument 403 /error.php?error=403
ErrorDocument 404 /error.php?error=404
ErrorDocument 406 /error.php?error=406
ErrorDocument 410 /error.php?error=410
</IfModule>

  上面的路徑適用於網站建立在主域名下如 http://yourdomain/,如網站建立在子層 http://yourdomain/website/,則每一路徑前面多加一層,即 /website/error.php?error=*** 。

完成:

錯誤頁面