【发布时间】:2015-01-06 03:02:10
【问题描述】:
我将 PDO 与 try 和 catch 一起使用,因此我可以将错误捕获到用户定义的函数中。我想知道这种方法是否可以接受。
试一试:
Try
{
...
} catch (Exception $e) {
// Proccess error
$msg = $e->getMessage();
$timestamp = date("Y-m-d H:i:s");
$line = $e->getLine();
$code = $e->getCode();
handle_error($msg, $timestamp, $line, $code);
die("oops! It's look like we got an error here! Try Again!");
}
错误处理函数:
function handle_error($msg, $timestamp, $line, $code) {
$file = 'errorlog.txt';
$data = "$timestamp // Error Message: $msg | Error Code: $code | Error Line: $line \n";
file_put_contents($file, $data, FILE_APPEND);
return;
}
谢谢
【问题讨论】:
-
如果可以的话是什么意思?在我看来,如何处理错误取决于您自己。
-
如果它有效,满足您的意图并通过所有代码设计要求 - 没关系。如果没有 - 那就不行了。
-
我的意思是,这足够安全吗?对于真实网站来说,这是一个不错的错误处理,还是我需要做其他事情?
-
好吧,它没有暴露任何明显的漏洞。您只是在写入文本文件。该文件位于何处?是否可以通过网络服务器直接访问?
-
嗯,我还在测试。我想将此错误日志放在服务器中的某个位置,例如 /var/logs/。
标签: php pdo error-handling