简体   繁体   English

重现PHP未定义索引错误

[英]Reproduce PHP undefined index error

[Mon Nov 11 11:07:18 2013] [error] [client nnn.nnn.nnn.nnn] PHP Notice:  Undefined index: population in /home/client/locale-qa/include/header.php on line 11

I'm attempted to reproduce this log error from a 300+MB Apache log file my vendor sent. 我试图从供应商发送的300 + MB Apache日志文件中重现此日志错误。 The vendor cannot provide the code found in header.php (frustrating), but this error, and many like it seems to be at the root of an HTTP response we are getting on our side. 供应商无法提供在header.php找到的代码(令人沮丧),但是该错误以及许多类似的错误似乎是我们站在我们这边的HTTP响应的根源。

I'm not a PHP-ninja, but I understand the error enough to see it suggests that some associative index label may not be defined. 我不是PHP忍者,但我理解该错误足以使它表明可能未定义某些关联索引标签。

Anything simple that would allow me to reproduce the error in a sandbox Apache build I have would be helpful. 任何能让我在沙盒Apache构建中重现错误的简单方法都将有所帮助。

<?php
echo $_REQUEST["population"]; //Undefined index: population 

//Solution  

$population = isset( $_REQUEST["population"] ) ? $_REQUEST["population"] : null; //if not set then set it to null
?>

Line 11 of header.php references $somearray['population'] , but there is no 'population' key in that array. header.php的第11行引用$somearray['population'] ,但该数组中没有“ population”键。

You will need to look at header.php to see how it's populating $somearray , and why it might not be providing a value for 'population'. 您将需要查看header.php以查看它如何填充$somearray ,以及为什么它可能没有为“ population”提供值。

It's often a good idea to use isset($somearray['population']) or array_key_exists('population', $somearray) to avoid an "undefined index" error, but you'll have to decide what the script should do if the key does not exist in the array. 通常最好使用isset($somearray['population'])array_key_exists('population', $somearray)以避免“未定义的索引”错误,但是如果键在阵列中不存在。

Try grep -ing for the timestamp and the ip in the log file, that should narrow it down enough so you can try them all and see if the error surfaces (hoping that this was a GET request). 尝试grep -ing作为时间戳和日志文件中的ip,这应该将其范围缩小到足够,以便您可以全部尝试并查看错误是否浮出水面(希望这是GET请求)。 A 300M+ file should not be a problem for that. 一个300M +的文件应该不是问题。 The for the usual apache log format this would look something like this: 对于通常的Apache日志格式,它看起来像这样:

grep -iE "x\.x\.x\.x.*\[11/\Nov\/2013:11:07:19" the_log_file.log

(the x-es where the ip address goes). (IP地址所在的x-es)。 You should see something like this at the end of the lines: 您应该在行尾看到以下内容:

.... GET /some_path/some.php?foo=bar HTTP/1.1" 200 42

From this, you can grab the path, append it you your server's domain and see if the error is there or not. 通过此操作,您可以获取路径,将其附加到服务器的域中,然后查看错误是否存在。 You can ignore the image, css and other static asset GETs, but if you have some rewrite rules or other way of PHP being executed on those lines too then check those as well. 您可以忽略图像,css和其他静态资产GET,但是如果您也有一些重写规则或在这些行上执行PHP的其他方式,则也请对其进行检查。

If you are on a unix like system you probably have grep already, if you are not you can grab a copy for windows here: http://gnuwin32.sourceforge.net/packages/grep.htm 如果您使用的是类似Unix的系统,则可能已经有grep;如果您没有,则可以在此处获取Windows的副本: http : //gnuwin32.sourceforge.net/packages/grep.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM