321dw 发表于 2016-3-7 09:46:17

PHP 错误处理机制

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Make Error Happen!</title>
</head>
<body>
<form action = "error_.php" method = "post">
    <table border ="0">
      <tr>
            <td>MESSAGE:</td>
            <td><input type="text" maxlength="50" size="30" name="error_msg"/></td>

      </tr>
      <tr>
            <td>
            <select name="error">
            <option value="256"> E_USER_ERROR </option>
            <option value="512">E_USER_WARNING</option>
            <option value="1024">E_USER_NOTICE</option>>
            </select>
            </td>


      </tr>
      <tr>
            <td colspan = "2" algin = "center">
                <input type = "submit" value = "submit error"/>
            </td>
      </tr>
    </table>
</form>
</body>
</html>
<?php
//错误处理函数
    functionerror_handle($error_level, $error_msg)
    {
      echo"an error is cause by user and the error level is:".$error_level."error message is:".$error_msg;
      //调用error_log函数将错误信息写入日志(以追加形式)
      error_log(date('Y-m-d G-i-s').$error_level.$error_msg.'\n',3,'error_log.txt');
    }
    //设置错误处理函数,只处理由用户自己引起的错误
    set_error_handler("error_handle",E_USER_ERROR|E_USER_NOTICE|E_USER_WARNING);
    if(isset($_POST['error']) && isset($_POST['error_msg']))
    {
      $error_level = (int)$_POST['error'];
      $error_msg = $_POST['error_msg'];
      trigger_error($error_msg,$error_level);

    }

?>




运行示例




页: [1]
查看完整版本: PHP 错误处理机制