gpt4 book ai didi

php - PHP中发生错误时如何获取所有变量

转载 作者:行者123 更新时间:2023-12-03 07:52:52 24 4
gpt4 key购买 nike

我的PHP应用程序中有一个自定义错误处理程序,我想获取错误发生时定义的变量。

为此,我使用以下代码:

    function error_handler($errno, $errstr, $errfile, $errline){
..some stuff..
ob_start();
var_dump(get_defined_vars());
$environment = ob_get_clean();
..some other stuff..
}

发生的情况是,它仅返回在error_handler中定义的变量。我想要的是这段代码返回所有变量,而不仅仅是返回error_handler中定义的变量。这里是否存在我不了解的范围问题,或者该问题来自其他地方?

最佳答案

我通过创建一个文件来修复它,该文件将错误之前与自定义错误处理结合起来存储在每个变量中。

getvars.php :(请注意,这不是一个函数,只是您包含在页面中的一些代码)

<?php
$variables = get_defined_vars();
$ignore = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', '_SERVER', '_ENV', 'ignore');
// diff the ignore list as keys after merging any missing ones with the defined list
$vars = array_diff_key($variables + array_flip($ignore), array_flip($ignore));
$values="";
foreach ($vars as $var => $value){
if(!is_array($value) && !is_object($value)){
$values.=$var."=".strval($value).";";
}
}
//create the file
$ip=$_SERVER['REMOTE_ADDR'];
$filename=$_SERVER['DOCUMENT_ROOT']."/tmp/".$ip.".txt";
$tmpFile = fopen($filename,"wb");
fwrite($tmpFile,$values);
fclose($tmpFile);
?>

error_log.php :(自定义错误处理程序)
<?php
set_error_handler("errorHandler");
register_shutdown_function("shutdownHandler");
function errorHandler($error_level, $error_message, $error_file, $error_line, $error_context){
$error = $error_message." | ".$error_file." | line:".$error_line;
switch ($error_level) {
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_PARSE:
mylog($error, "(fatal)");
break;
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
mylog($error, "(error)");
break;
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
mylog($error, "(warn)");
break;
case E_NOTICE:
case E_USER_NOTICE:
mylog($error, "(info)");
break;
case E_STRICT:
mylog($error, "(debug)");
break;
default:
mylog($error, "(warn default)");
}
}
function shutdownHandler(){ //will be called when php script ends.
$lasterror = error_get_last();
switch ($lasterror['type']){
case E_ERROR:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
case E_RECOVERABLE_ERROR:
case E_WARNING:
case E_CORE_WARNING:
case E_COMPILE_WARNING:
case E_USER_WARNING:
case E_NOTICE:
case E_USER_NOTICE:
case E_STRICT:
case E_PARSE:
$error = $lasterror['message']." | ".$lasterror['file']." | line:".$lasterror['line'];
mylog($error, "fatal");
}
}
function mylog($error, $errlvl){
$ip=$_SERVER['REMOTE_ADDR'];
error_log($error." ".$errlvl."\n"."https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI] ",0);
$filename=$_SERVER['DOCUMENT_ROOT']."/tmp/".$ip.".txt";
if (file_exists($filename)){ //read the file and add it to the log
$file=fopen($filename, "r");
$variables=fread($file, filesize($filename));
error_log("Variables: ".$variables,0);
}
}
?>

例:
<?php
$a=1;
$b=0;
include("./getvars.php");
require_once("./error_log.php");
echo $a/$b; //an error :-)
?>

我希望这对某人仍然有用:-)

关于php - PHP中发生错误时如何获取所有变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com