如果发生错误,通过php重定向到另一个页面?

时间:2013-07-08 12:50:45

标签: php redirect error-handling

我必须检查天气页面是否有错误,如果有错误,我必须重定向到一个默认页面,如 error.php

我尝试过以下链接,但我无法得到结果

我的 .htaccess

中的更改
ErrorDocument 400 /errors.php
ErrorDocument 403 /errors.php
ErrorDocument 404 /errors.php
ErrorDocument 405 /errors.php
ErrorDocument 408 /errors.php
ErrorDocument 500 /errors.php
ErrorDocument 502 /errors.php
ErrorDocument 504 /errors.php

我的 errors.php 代码

<?php
$urPath= $_SERVER['PHP_SELF'];
$fle =basename($urPath);
$finstr =str_replace("$fle","","$urPath");
$status = $_SERVER['REDIRECT_STATUS'];
$codes = array(
       400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
       403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
       404 => array('404 Not Found', 'The page you requested was not found on this server.'),
       405 => array('405 Method Not Allowed', 'The method specified in the request is not allowed for the specified resource.'),
       408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
       500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
       502 => array('502 Bad Gateway', 'The server received an invalid response while trying to carry out the request.'),
       504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),
);

$title = $codes[$status][0];
$message = $codes[$status][1];
if ($title == false || strlen($status) != 3) {
       $message = 'Please supply a valid HTTP status code.';
}

echo '<h1>Hold up! '.$title.' detected</h1>
<p>'.$message.'</p>';
?>
<!DOCTYPE html> 
<html> 
<head> 
    <title>Error</title> 
    <meta name="apple-mobile-web-app-capable" content="yes" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="<?php echo $finstr;?>css/token-input.css" rel="stylesheet" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/app-reset.css" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/jquerymobilecrm.min.css" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/jqm-datebox.css" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/dcr_css.css" />
    <link rel="stylesheet" href="<?php echo $finstr;?>css/jQueryValidationEngine.css" />
    <script src="<?php echo $finstr;?>js/jquery-1.8.1.min.js"></script>
    <script src="<?php echo $finstr;?>js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/jquery.tokeninput.js"></script>
    <script src="<?php echo $finstr;?>js/jquery.mobile-1.2.0.min.js"></script>
    <script src="<?php echo $finstr;?>js/logout.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/jqm-datebox.core.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/jqm-datebox.mode.datebox.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/jQueryValidationEngine-en.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/jQueryValidationEngine.js"></script>
    <script type="text/javascript" src="<?php echo $finstr;?>js/dateValidation.js"></script>
    <style type="text/css">
        .data-clear{
        height:35px;
        width:100%;
        }
    </style>    
    <?php //require_once"header.php";?>
</head> 
<body> 
<div data-role="page" id="addDialysis">
    <div data-role=header data-theme="a" data-position="fixed" style="height:40pt;">
        <h1 align="left" style="color: white;margin-top: 10pt;">Error Page</h1>
        <a href="home.php" data-icon="home" data-role="button" data-theme="reset" rel="external" style="margin-top:5pt;"><span style="padding-left: 14px;color: white;">Home</span></a>
    </div>
    <div data-role=fieldcontain>
            <div style="margin-left:45%"><p>Error Occurred !!!</p></div>
    </div>
</div>
</body>
</html>

建议我任何想法,提前谢谢..

1 个答案:

答案 0 :(得分:1)

您是否正在使用本地服务器/ localhost?

/errors.php将引用您的基本网址,例如: http://localhost/errors.php。如果您正在使用项目目录(例如http://localhost/project_dir/),则errors.php文件的路径应为/project_dir/errors.php。

通过此更改,代码在我的系统上正常工作。