欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

获取PHP警告错误信息的解决方法

程序员文章站 2022-05-21 17:18:37
代码如下所示:复制代码 代码如下:
代码如下所示:
复制代码 代码如下:

<?php
/**
* 更新非法字符、發送錯誤信息

* @author
* @copyright 2009-06-10
*/
error_reporting(e_error | e_warning | e_parse);
set_error_handler('displayerrorhandler');//自定義錯誤
//調試信息
echo "time:".date("y-m-d h:i:s")."\n";
//數據配置
//公共函數
function displayerrorhandler($error, $error_string, $filename, $line, $symbols)
{
    $error_no_arr = array(1=>'error', 2=>'warning', 4=>'parse', 8=>'notice', 16=>'core_error', 32=>'core_warning', 64=>'compile_error', 128=>'compile_warning', 256=>'user_error', 512=>'user_warning', 1024=>'user_notice', 2047=>'all', 2048=>'strict');
    $msg = sprintf("%s: %s at file %s(%s)", $error_no_arr[$error] ,$error_string, $filename, $line);
    if(in_array($error,array(1,2,4))){      
        echo $msg; echo "\n";//調試顯示
        //發送信息
        if($error==1||$error==2) {
            sendbankmsg($error_string); //發送簡訊
            if(strpos($error_string, 'xml_parse(): bytes:')!==false){
                writefiltefile($error_string);
            }
        }
    }
}
function hex2bin($hexdata) {
    $bindata = '';
    for($i=0; $i < strlen($hexdata); $i += 2) {
        $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
    }
    return $bindata;
}
function writefiltefile($error_string)
{
    if(strpos($error_string, 'xml_parse(): bytes:')===false||strpos($error_string, ' 0x')===false){
        return;
    }          
    //寫入文件
    $filename = 'filtetext.php';
    include($filename);
    $error_string = str_replace('xml_parse(): bytes:', '', $error_string);
    $error_string = str_replace(' 0x','', $error_string);
    $error_text = hex2bin($error_string);
    $filtetextarr[] = $error_text;
    $temparr = array_unique($filtetextarr);//去除重復
    $result = implode("','",$temparr);
    $result = "<?php\n\$filtetextarr = array('".$result."');\n?>";
    filewrite($filename, $result, 'w');  
}
function sendbankmsg($msg)
{
    $timestamp = time();
    $params = "msg=".$msg
             ."&posttime=".$timestamp;  
    $length = strlen($params);
    //创建socket连接
    $domain = "www.admin.com"; //socket域名
    $actionpath = "/action/bank/bankmsg.php"; //文件路徑
    $fp = fsockopen($domain,80);
    //构造post请求的头
    if($fp){
        $header = "post ".$actionpath." http/1.1\r\n";
        $header .= "host:".$domain."\r\n";
        $header .= "content-type: application/x-www-form-urlencoded\r\n";
        $header .= "content-length: ".$length."\r\n";
        $header .= "connection: close\r\n\r\n";
        //添加post的字符串
        $header .= $params."\r\n";
        //发送post的数据
        fputs($fp,$header);
        while (!feof($fp)) {
            $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
            if ($inheader && ($line == "\n" || $line == "\r\n")) {
                echo $line;
            }
        }
        fclose($fp);
    }
}
function filewrite($ffilename, $fcontent, $ftag = 'w') {
    ignore_user_abort (true);
    $fp = fopen($ffilename, $ftag);
    if (flock($fp, lock_ex)) {
        fwrite($fp, $fcontent);
        flock($fp, lock_un);
    }
    fclose($fp);
    ignore_user_abort (false);
    return;
}
?>