Undefined offset: 2 in /.../persistent_login.module on line 308

This message just started appearing today on a site I oversee and no changes have been made to Drupal to cause it. Any idea as to why this would happen?

thanks

Comments

gapple’s picture

Status: Active » Postponed (maintainer needs more info)

There shouldn't be any issue with an undefined array index there:
http://drupalcode.org/project/persistent_login.git/blob/refs/tags/6.x-1....

Can you confirm that you are really using persistent_login 6.x-1.4, without any modifications?

tmsimont’s picture

Confirmed -- it magically went away on clearing the site cache. I looked at that line, too, and I'm very confused about how there could be an undefined index.

The error seemed to pop up after I cleared the site cache, viewed a subdomain of the site, and then went back to the primary domain of the site. I have a feeling it has something to do with the cookie array.

I'll post back here if I see the message again...

tmsimont’s picture

Status: Postponed (maintainer needs more info) » Active

This issue has happened again on the same site. There are about 7000 users on the site, and a script runs every 30 minutes, activating and/or deactivating user account, and creates new accounts.

That script calls user_save for a lot of users. This might be doing something to trigger an alteration of the cookie.

I ran a few test scripts on the server to see what exactly in line 308 would produce that error.

There shouldn't be any issue with an undefined array index there:

true.. and there's not. The error is not "undefined array index" it's "undefined offset"

There's a difference. The difference is that the $_COOKIE[$cookie_name] is set and not empty, so it passes your check:
if ($user->uid == 0 && isset($_COOKIE[$cookie_name]) && !empty($_COOKIE[$cookie_name]) && !isset($_SESSION['persistent_login_check'])) ...

But somehow, the cookie does not have enough ":" characters, so the error is with the list() function trying to pull 3 values from an array with 1.

Take this example code, which throws the exact same error on the same server:


$test = array('key'=>'a:b:c','foo'=>' ');
list($uid, $series, $token) = explode(':', $test['foo']);
echo "<pre>uid: $uid\n series:$series\n token:$token</pre>";


In this code, $test['foo'] is set and not empty, but when run through your checker it passes and then throws up the exact notices that I'm seeing persistent_login throw.

Somehow the cookie value is not set properly, and then the code hits line 308 and blows up.

By replacing line 308 as done in the following example, the error won't show up, but will get sent to the logs. I can't reproduce this issue, but if I see this in the logs I'll let you know what value the cookie has when it occurs:

line 308 from:

list($uid, $series, $token) = explode(':', $_COOKIE[$cookie_name]);

to:

	
	$cookie_array = explode(':', $_COOKIE[$cookie_name]);
	if(count($cookie_array)==3){
		list($uid, $series, $token) = $cookie_array;
	}else{
		watchdog('peristent_login', 'Mysterious malformed cookie: <pre>'.print_r($_COOKIE[$cookie_name],true).'</pre>.', WATCHDOG_ERROR);
	}
gapple’s picture

Status: Active » Postponed (maintainer needs more info)

Undefined index / offset indicates if the provided key was a string or integer, respectively. I would have expected list() to throw a distinct error if it was not provided enough values, but it make sense.

Very strange. The cookie value is user-supplied, so it shouldn't be affected by any cron tasks (though the token may no longer be valid if the cron task invalidates it in some way).

I would also suggest adding a return statement if there are not enough segments in the cookie value so that later code doesn't error with undefined variables, if you haven't already.

Thank you for your efforts digging into this issue.

gapple’s picture

Issue summary: View changes

fixed unterminated blockquote

gapple’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

This shouldn't be a breaking error, and without any further information on the cause of the malformed cookies I'm closing this issue.