ĐIỀU KHIỂN VÀ TRÁNH NHỮNG LỖI - Pdf 62

ĐIỀU KHIỂN VÀ TRÁNH NHỮNG LỖI
Sau khi viết mã lệnh, người mới lập trình sẽ gặp không ít khó khăn trong việc tìm ra
lỗi và chỉnh sửa lỗi. PHP hỗ trợ đầy đủ tính năng lập trình giao diện API (Applications
Programming Interface) có thể tìm ra lỗi và khắc phục những lỗi ấy. Ngoài PHP thì Apache
Web Server cũng hỗ trợ người lập trình tìm ra lỗi và khắc phục lỗi.
1.1. Tiếp xúc với lỗi Apache Web Server như thế nào?
Apache có một thư mục Lỗi tài liệu, bạn có thể định cấu hình trong file httpd.conf
để tạo ra những trang thông báo lỗi với PHP, vì thế người thăm trang web không thấy buồn
chán. PHP tạo ra trang web thông báo lỗi thân thiện cho người dùng.
Không giống những trang thông báo lỗi như PHP, trang của Apache được sử dụng
nhiều cho những trang đã mất.
1.1.1. Chỉ thị lỗi của Apache
Ví dụ về việc sử dụng phương pháp Apache.
Trước hết cần thay đổi trong thư mục httpd.conf để cho phép tạo ra một trang thông
báo lỗi, Apache thường được mặc định bên trong những trang lỗi. Nhưng bạn muốn có một
thông báo như ý muốn khi có lỗi xuất hiện.
Bạn cần có những thay đổi sau:
1. Mở file httpd.conf, những dòng 750 hoặc xa hơn, tìm vài dòng giống như sau:
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 “The server made a boo boo.”
#ErrorDocument 404 /missing.html
#ErrorDocument 404 “/cgi-bin/missing_handler.pl”
#ErrorDocument 402 195Hhttp://www.example.com/subscription_info.html
2. Thay đổi thông tin cho phép sau đó khởi động lại Apache
# Customizable error responses come in three flavors:
- 1 -
# 1) plain text 2) local redirects 3) external redirects
#

Request<br>”;
$error_output .= “<a
href=\”mailto:[email protected]\”>” .
“Contact</a> the system administrator”;
$error_output .= “ if you feel this to be in error”;
break;
case 401:
$error_output = “<h1>&quot;Authorization
Required&quot; “ .
“Error Page - (Error Code 401)</h1>”;
$error_output .= “You have supplied the wrong
information to “ .
“access a secure area<br>”;
$error_output .= “<a
href=\”mailto:[email protected]\”>” .
“Contact</a> the system administrator”;
$error_output .= “ if you feel this to be in error”;
break;
case 403:
$error_output = “<h1>&quot;Forbidden Access&quot;
Error Page - “ .
“(Error Code 403)</h1>”;
$error_output .= “You are denied access to this
area<br>”;
$error_output .= “<a
href=\”mailto:[email protected]\”>” .
“Contact</a> the system administrator”;
$error_output .= “ if you feel this to be in error”;
break;
- 3 -

?>
- 4 -
<html>
<head>
<title>Beginning PHP5, Apache, MySQL Web Development</title>
</head>
<body>
<?php
echo $error_output;
?>
</body>
</html>
3.Mở trình duyệt và vào 196Hhttp://localhost/asdf/qwerty/page.html hoặc bất kỳ trang
web nào mà bạn biết là không thể vào thăm trang web, bạn sẽ nhìn thấy như hình 9.1
Hình 9
1. Cách khác để kiểm tra rằng đoạn mã của bạn được ứng dụng vào
trong trang web với chuỗi thông tin truy vấn cho người dùng. Ví dụ
mở trình duyệt với tên 197Hhttp://localhost/error.php?500 , trang
sẽ sử dụng truy vấn và chạy đoạn mã nếu có lỗi nó sẽ xuất
hiện như hình sau ( giống hình trước).
- 5 -
Hình 9.2
Nó hoạt động thế nào?
Bạn vừa tạo ra những thông báo lỗi thường gặp, bằng việc sử dụng switch(), bạn có thể
hiển thị thích hợp những thông báo lỗi cần thiết cho người dùng.
1.1.2. Lỗi tài liệu của Apache: Trang báo lỗi thông thường
Bạn chỉ cho người dùng nhìn thấy những lỗi thông thường, mặc dù bạn có thể làm
nhiều hơn thế, đây là cách tốt để bạn theo dõi những trang mà bạn không thể kiểm tra định
kỳ được.
Ví dụ tạo một E_mail lỗi

$headers .= “From: Apache Error <[email protected]>\r\n”;
$headers .= “Cc: [email protected]\r\n”;
mail($to, $subject, $body, $headers);
}
$date = getdate();
$full_date = $date[‘weekday’] . “, “ .
$date[‘month’] . “ “ .
$date[‘mday’] . “, “ .
$date[‘year’];
$full_time = $date[‘hours’] . “:” .
$date[‘minutes’] . “:” .
$date[‘seconds’] . “:” .
$date[‘year’];
$error_no = $_SERVER[‘QUERY_STRING’];
$request_page = $_SERVER[‘REQUEST_URI’];
- 7 -
switch ($error_no)
{
case 400:
$error_output = “<h1>\”Bad Request\” Error Page - “ .
“(Error Code 400)</h1>”;
$error_output .= “The browser has made a Bad Request<br>”;
$error_output .= “<a href=\”mailto:[email protected]\”>” .
“Contact</a> the system administrator”;
$error_output .= “ if you feel this to be in error”;
email_admin($error_no,
$error_output,
$full_date,
$full_time,
$request_page);

$error_output = “<h1>\”Page Not Found\” Error Page - “ .
“(Error Code 404)</h1>”;
$error_output .= “The page you are looking for “ .
“cannot be found<br>”;
$error_output .= “<a href=\”mailto:[email protected]\”>” .
“Contact</a> the system administrator”;
$error_output .= “ if you feel this to be in error”;
email_admin($error_no,
$error_output,
$full_date,
$full_time,
$request_page);
break;
case 500:
$error_output = “<h1>\”Internal Server Error\” Error Page - “ .
“(Error Code 500)</h1>”;
$error_output .= “The server has encountered “ .
“an internal error<br>”;
$error_output .= “<a href=\”mailto:[email protected]\”>” .
- 9 -


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status