《PHP与MySQL程序设计》第八章异常处理


8.2 错误日志

1. Web服务器进程所有者必须有足够的权限来写日志文件到某个目录。
2. 确保这个文件存放在文档根之外,以减少遭到攻击的可能性。
3. 可以写入操作系统的日志工具(Linux上是syslog,Windows上是Event Viewer)。

<?php
     define_syslog_variables();
     openlog("CHP8", LOG_PID, LOG_USER);
     syslog(LOG_WARNING, "Chapter 8 example warning");
     closelog();
?>


int openlog(string ident, int option, int facility);

ident:消息标识符。
option:


facility:指定LOG_CRON将后续的消息发送到cron日志,指定LOG_USER使消息发送到messages文件。





8.3 异常处理

try {
     $fh = fopen("contacts.txt", "r");
     if (! $fh) {
          throw new Exception("could not open the file!");
     }
} catch (Exception $e) {
     echo "Error ".$e->getFile().", line".$->getLine().":".$e->getMessage();
}

posted on 2011-11-27 15:22  毛小娃  阅读(111)  评论(0编辑  收藏  举报

导航