A.H.P design
11-07-2012, 12:47
در این پست قصد دارم برای شما نحوه ساخت صفحه مدیریت خطای همه کاره رو به وسیله هدرها بهتون آموزش بدم. صفحه خطاهایی مثل خطای ۴۰۴ و …
یکی از مزیتهای این روش اینه که به وسیله یک صفحه میتونیم تمام خطاهای به وجود اومده رو مدیریت کنیم.
برای اینکار ابتدا در فایل htaccess کدهای زیر رو قرار میدیم.
1
ErrorDocument 400 /errors.php
2
ErrorDocument 403 /errors.php
3
ErrorDocument 404 /errors.php
4
ErrorDocument 405 /errors.php
5
ErrorDocument 408 /errors.php
6
ErrorDocument 500 /errors.php
7
ErrorDocument 502 /errors.php
8
ErrorDocument 504 /errors.php
حتما میدونید که دستور ErrorDocument هنگامی که یک خطا اتفاق میافته صفحه رو به error.php انتقال میده. مثلا هنگامی که یک صفحه پیدا نشه به جای خطای پیش فرض Not Found 404 میتونیم یک صفحه خطای دیگه رو نمایش بدیم.
به هر حال وقتی کد بالا رو در فایل htaccess وارد کردید یک فایل با نام errors.php بسازید و کد زیر رو داخلش قرار بدید .
1
$status = $_SERVER['REDIRECT_STATUS'];
2
$codes = array(
3
400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
4
403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
5
404 => array('404 Not Found', 'The page you requested was not found on this server.'),
6
405 => array('405 Method Not Allowed', 'The method specified in the request is not allowed for the specified resource.'),
7
408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
8
500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
9
502 => array('502 Bad Gateway', 'The server received an invalid response while trying to carry out the request.'),
10
504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),
11
);
12
13
$title = $codes[$status][0];
14
$message = $codes[$status][1];
15
if ($title == false || strlen($status) != 3) {
16
$message = 'Please supply a valid HTTP status code.';
17
}
18
19
echo '<h1>Hold up! '.$title.' detected</h1>
20
<p>'.$message.'</p>';
فکر میکنم همه چیز واضح و روشنه ولی بازم اگر سوالی دارید بپرسید.
یکی از مزیتهای این روش اینه که به وسیله یک صفحه میتونیم تمام خطاهای به وجود اومده رو مدیریت کنیم.
برای اینکار ابتدا در فایل htaccess کدهای زیر رو قرار میدیم.
1
ErrorDocument 400 /errors.php
2
ErrorDocument 403 /errors.php
3
ErrorDocument 404 /errors.php
4
ErrorDocument 405 /errors.php
5
ErrorDocument 408 /errors.php
6
ErrorDocument 500 /errors.php
7
ErrorDocument 502 /errors.php
8
ErrorDocument 504 /errors.php
حتما میدونید که دستور ErrorDocument هنگامی که یک خطا اتفاق میافته صفحه رو به error.php انتقال میده. مثلا هنگامی که یک صفحه پیدا نشه به جای خطای پیش فرض Not Found 404 میتونیم یک صفحه خطای دیگه رو نمایش بدیم.
به هر حال وقتی کد بالا رو در فایل htaccess وارد کردید یک فایل با نام errors.php بسازید و کد زیر رو داخلش قرار بدید .
1
$status = $_SERVER['REDIRECT_STATUS'];
2
$codes = array(
3
400 => array('400 Bad Request', 'The request cannot be fulfilled due to bad syntax.'),
4
403 => array('403 Forbidden', 'The server has refused to fulfil your request.'),
5
404 => array('404 Not Found', 'The page you requested was not found on this server.'),
6
405 => array('405 Method Not Allowed', 'The method specified in the request is not allowed for the specified resource.'),
7
408 => array('408 Request Timeout', 'Your browser failed to send a request in the time allowed by the server.'),
8
500 => array('500 Internal Server Error', 'The request was unsuccessful due to an unexpected condition encountered by the server.'),
9
502 => array('502 Bad Gateway', 'The server received an invalid response while trying to carry out the request.'),
10
504 => array('504 Gateway Timeout', 'The upstream server failed to send a request in the time allowed by the server.'),
11
);
12
13
$title = $codes[$status][0];
14
$message = $codes[$status][1];
15
if ($title == false || strlen($status) != 3) {
16
$message = 'Please supply a valid HTTP status code.';
17
}
18
19
echo '<h1>Hold up! '.$title.' detected</h1>
20
<p>'.$message.'</p>';
فکر میکنم همه چیز واضح و روشنه ولی بازم اگر سوالی دارید بپرسید.