简体   繁体   中英

Php undefined index notice when reading array from cookie

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.
       );

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, ie before the <html> tag):

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

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']));

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'];

I get a warning in my error_log saying that:

PHP Notice:  Undefined index: ID

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 , ie, 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?

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

PS: 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

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.

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.

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.

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 ... 2 days of my life I'll never get back!

Sorry about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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