从Cookie读取数组时的PHP未定义索引通知 [英] Php undefined index notice when reading array from cookie

查看:47
本文介绍了从Cookie读取数组时的PHP未定义索引通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个很奇怪的问题,并且我坐在那里已经有一段时间没有成功。我的页面结构大致如下:

OK I have a rather odd problem and I have been sitting on it for some time without success. My page structure is roughly as follows:

<?php
ob_start();
error_reporting(E_ALL);

然后我用用户信息创建一个数组,例如:

then I create an array with user info like:

$USER = array('ID' => md5(uniqid(rand(),1)),
          'referrer' => $_SERVER['HTTP_REFERER'],
          etc.
       );

这大约有10个条目。
然后,我检查用户是否已经设置了我的cookie,如果没有,我就设置了(仍然在输出任何内容之前,即在< html> 标记):

This will have like 10 entries or so. Then I check whether the user already has my cookie set, and if they don't, I do (still before anything is output, i.e. before the <html> tag):

$data = base64_encode(serialize($USER));
setcookie('cookiename', $data, time()+86400*30, '/','mydomain.com',1,1);

到目前为止很好。现在说用户然后导航到我网站上的另一个页面。现在已经设置了cookie,因此,如果支票返回 true ,我将通过以下方式读取cookie:

So far so good. Now say the user then navigates to another page on my site. Now the cookie will already be set, so if the check returns true, I read the cookie with:

$DUMMY = unserialize(base64_decode($_COOKIE['cookiename']));

因此,现在$ DUMMY应该具有与以前写入Cookie相同的数组结构。真正令人困惑的是,当我尝试访问时:

So now $DUMMY should have the same array structure that was written to the cookie previously. The really baffling thing is that when I try to access:

$uid = $DUMMY['ID'];

我在error_log中收到一条警告,说:

I get a warning in my error_log saying that:

PHP Notice:  Undefined index: ID

其中真的很奇怪,因为(1)仅在存在cookie的情况下才读取cookie,并且如果存在,它首先应具有'ID'元素,因为它首先被写入了它(我确实对double和Triple进行了检查例如使用 print_r($ DUMMY); ,所有元素都在那里)。另外,当我随后尝试使用 $ uid 我可以时,即它确实具有应有的价值。因此,一切似乎都正常,只不过我的error_log文件获得了1000条这些PHP警告消息。有什么想法吗?

Which is really weird because (1) it will only read the cookie if the cookie exists, and if it exists, it should have the 'ID' element as it was written to it in the first place (and I do have double and triple checked this with print_r($DUMMY); for instance, and all the elements are there). In addition, when I then try to use $uid I can, i.e., it does have the value it should. So everything seems OK, except that my error_log file gets 1000s of these PHP warning messages. Any ideas?

我在页面页脚btw的最后执行 ob_end_flush();
谢谢。

I do the ob_end_flush(); at the very end of the page footer btw. Thanks.

P.S .:还有一些奇怪的事情在发生。
出于测试目的,我添加了一些IP过滤器,因此仅当我访问该页面时才加载cookie脚本。这似乎可行,因为在数据库中没有进行其他输入。但是,错误消息会在error_log文件中不断滴答作响。我提供摘录:

P.S.: Some more odd things are going on. OK for testing purposes, I have added some IP filter, so the cookie script is only loaded if I am the one visiting the page. It seems to work, because no further entries have been made in the database. Nevertheless, the error messages keep ticking away in the error_log file. I am providing an excerpt:

[13-Dec-2017 01:04:34 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:04:35 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:05:06 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:05:07 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:05:38 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:05:39 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:06:10 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:06:11 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:06:42 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:06:43 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:07:14 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:07:15 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:07:46 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:07:47 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:08:18 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:08:20 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:08:50 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:08:51 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:09:22 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:09:23 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:09:54 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:09:55 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:10:26 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:10:27 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:10:58 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:10:59 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:11:30 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:11:31 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:12:03 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:12:03 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:12:35 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:12:35 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:13:07 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:13:07 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:13:39 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:13:39 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:14:11 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:14:11 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:14:43 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:14:43 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:15:15 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:15:15 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:15:47 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:15:47 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:16:19 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:16:19 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:16:51 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:16:51 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:17:23 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:17:23 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:17:55 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:17:55 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:18:27 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51
[13-Dec-2017 01:18:27 Europe/Berlin] PHP Notice:  Undefined index: ID in /home/xxxxxxxx/public_html/include/usertrack.inc.php on line 51

有两件事让我感到奇怪:
1)为什么我首先看到它们,因为脚本仅包含在我的IP中,而
2)这些消息有些奇怪。每30秒2次点击。

Two things strike me as odd there: 1) Why am I seeing them in the first place since the script is only included with my IP, and 2) there is some odd regularity about these messages. 2 hits about every 30 seconds.

这是什么?某种黑客攻击?但是,即使这是一些奇怪的DDoS,攻击者为何仍会看到cookie脚本?我的IP过滤器是这样的:

What is this? Some kind of hack attack? But even if it was some weird DDoS, why would the attacker see the cookie script? My IP filter is this:

if ($_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx') { // the x's would be my IP
    require 'include/usertrack.inc.php';
    flush();
}

这对我来说绝对是莫名其妙。

This is an absolute mistery to me.

推荐答案

哦,伙计……我很难过。我刚刚解决了。

oh man ... I feel bad. I just solved it.

好的,我最近对目录结构做了一些更改,并且正在运行自己的云服务器(使用owncloud)。由于目录结构的更改,owncloud不再找到应该与之同步的目录。因此,基本上,它每30秒尝试与服务器联系以查找目录。由于它来自我的机器,所以owncloud请求确实具有我的IP编号,因此通过了IP过滤器,但是由于无法找到目录,它必须触发了404或某些操作。

OK, I did some recent changes to my directory structure and I am running my own cloud server (using owncloud). Due to the change in directory structure, owncloud no longer found the directory it was supposed to sync with. So basically, every 30 seconds it tried to contact the server to locate the directory. Since it came from my machine, the owncloud request did have my IP number so passed the IP filter but since it could not locate the directory it must have triggered some 404 or something.

我不确定owncloud发出什么样的请求,但它一定触发了cookie脚本,甚至接受了cookie,但也许不允许我的脚本读出Cookie或在设置的同一秒将其删除。无论哪种方式,既然我再次将owncloud指向正确的目录,错误消息就会停止出现,并且error_log是干净的。

I am not sure what kind of request owncloud makes but it must have triggered the cookie script, maybe even accepted the cookie but maybe did not allow my script to read out the cookie or deleted it the same second it was set or something. Either way, now that I pointed owncloud to the correct directory again, the error messages stop appearing and the error_log is clean .

man ...我生命的第二天永远不会回来!

man ... 2 days of my life I'll never get back!

很抱歉。

这篇关于从Cookie读取数组时的PHP未定义索引通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆