To achieve this goal you can make use of the function set_error_handler in conjunction with the function debug_backtrace
I did some research and found a solution in neighboring stackoverflow, follows the code:
<?php
function process_error_backtrace($errno, $errstr, $errfile, $errline, $errcontext) {
if(!(error_reporting() & $errno))
return;
switch($errno) {
case E_WARNING :
case E_USER_WARNING :
case E_STRICT :
case E_NOTICE :
case E_USER_NOTICE :
$type = 'warning';
$fatal = false;
break;
default :
$type = 'fatal error';
$fatal = true;
break;
}
$trace = array_reverse(debug_backtrace());
array_pop($trace);
if(php_sapi_name() == 'cli') {
echo 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
foreach($trace as $item)
echo ' ' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()' . "\n";
} else {
echo '<p class="error_backtrace">' . "\n";
echo ' Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ':' . "\n";
echo ' <ol>' . "\n";
foreach($trace as $item)
echo ' <li>' . (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()</li>' . "\n";
echo ' </ol>' . "\n";
echo '</p>' . "\n";
}
if(ini_get('log_errors')) {
$items = array();
foreach($trace as $item)
$items[] = (isset($item['file']) ? $item['file'] : '<unknown file>') . ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ' calling ' . $item['function'] . '()';
$message = 'Backtrace from ' . $type . ' \'' . $errstr . '\' at ' . $errfile . ' ' . $errline . ': ' . join(' | ', $items);
error_log($message);
}
if($fatal)
exit(1);
}
set_error_handler('process_error_backtrace');
?>
You want to save the line content that gave error?
– Marcelo de Andrade
that’s right. It’s possible?
– Hugo Borges
I can’t answer your question, but the log itself is already a help for an attacker to understand how the system works so putting the source code even more exposed your system gets. What is the real need to put the source code in the log? The line number is not enough?
– lazyFox
Open the file with the normal functions(fopen) and skip to the line you indicate in the standard log.
– Lucas Torres
@lazyFox but as an attacker has access to the log? I thought only I had access to it since it gets in a folder where I access via ftp. I use it to identify possible problems with the system.
– Hugo Borges
@Lucastorres didn’t understand very well what you asked me to do
– Hugo Borges
See, instead of the standard "error," you want exactly the content of the line informed in the log, right?
– Lucas Torres
did not want the error is the line content, I thought to create a custom function to create log, this I can even do, the problem is how to get the function when there is an error in php.
– Hugo Borges
Try this article: How to perform error Handling In the section Create a custom error Handler
– lazyFox
I also advise you to see debug_backtrace
– lazyFox