您没有足够的信息来解决您的问题。尝试将完整的堆栈跟踪和请求信息添加到错误日志中以调查问题。
您可以创建自己的自定义错误处理程序,以将堆栈跟踪和请求信息添加到错误日志中。
set_error_handler('wpse_120959_handle_error');
function wpse_120959_handle_error( $errno, $errstr, $errfile, $errline ) {
if( $errno === E_USER_NOTICE ) {
$error = PHP_EOL . PHP_EOL;
$error .= sprintf('You have an error notice: "%s" in file "%s" at line: "%s".', $errstr, $errfile, $errline);
$error .= wpse_120959_generate_stack_trace() . PHP_EOL;
$error .= 'Request information:' . PHP_EOL;
$error .= wpse_120959_generate_http_request_info();
error_log( $error );
}
}
// Function from php.net http://php.net/manual/en/function.debug-backtrace.php#112238
function wpse_120959_generate_stack_trace() {
$e = new \Exception();
$trace = explode( "\n" , $e->getTraceAsString() );
// reverse array to make steps line up chronologically
$trace = array_reverse( $trace );
array_shift( $trace ); // remove {main}
array_pop( $trace ); // remove call to this method
$length = count( $trace );
$result = array();
for ($i = 0; $i < $length; $i++) {
$result[] = ($i + 1) . ')' . substr( $trace[$i], strpos( $trace[$i], ' ' ) ); // replace '#someNum' with '$i)', set the right ordering
}
$result = implode("\n", $result);
$result = "\n" . $result . "\n";
return $result;
}
function wpse_120959_generate_http_request_info() {
$request_info = '';
$request_info_format = "%s %s %s\nHTTP headers:\n";
$headers = wpse_120959_get_headers();
$header_format = "%s: %s \n";
$request_method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING );
$request_uri = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_STRING );
$server_protocol = filter_input( INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING );
$request_info = sprintf( $request_info_format, $request_method, $request_uri, $server_protocol );
foreach ( $headers as $name => $value ) {
$request_info .= sprintf( $header_format, $name, $value );
}
return $request_info;
}
function wpse_120959_get_headers() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (preg_match('/^HTTP_/', $name ) ) {
$value = filter_input( INPUT_SERVER, $name, FILTER_SANITIZE_STRING );
$name = strtr( substr( $name, 5 ), '_', ' ');
$name = ucwords( strtolower( $name ) );
$name = strtr( $name, ' ', '-' );
// add to list
$headers[$name] = $value;
}
}
return $headers;
}
您可以通过在代码中的某个位置(例如在single.php
文件中)添加trigger_error来检查其是否正常工作。
trigger_error('Annoying notice');
您的错误日志应输出如下内容:
2018/07/02 14:26:00 [error] 714#714: *58 FastCGI sent in stderr: "PHP message:
You have an error notice: "Annoying notice" in file "/var/www/test/content/themes/twentyseventeen/single.php" at line: "19".
1) /var/www/test/index.php(17): require('/var/www/test/w...')
2) /var/www/test/wordpress/wp-blog-header.php(19): require_once('/var/www/test/w...')
3) /var/www/test/wordpress/wp-includes/template-loader.php(74): include('/var/www/test/c...')
4) /var/www/test/content/themes/twentyseventeen/single.php(19): trigger_error('Annoying notice')
5) [internal function]: wpse_120959_handle_error(1024, 'Annoying notice', '/var/www/test/c...', 19, Array)
Request information:
GET /hello-world/ HTTP/1.1
HTTP headers:
Accept-Language: en-US;q=0.8,en;q=0.7
Accept-Encoding: gzip, deflate
Referer: http://test.local/
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
User-Agent: Mozilla/5.0 AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1
Upgrade-In" while reading upstream, client: 192.168.10.1, server: test.local, request: "GET /hello-world/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "test.local", referrer: "http://test.local"
通过这种消息,将更容易发现问题出在哪里。