0

I checked all 29 previous posts tagged with array_key_exists and I cannot find a specific question answered that deals with my issue. Our server has recently been updated and we've advanced to PHP 5.2.17 (and yes I know that's still behind, but we're fixing issues as we continue to advance, and 5.3 caused way too many issues for handling at once, let alone 5.4).

Our webpages are throwing an error message tied to array_key_exists:

[ERROR][2][array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object]

else if(array_key_exists("ACCOUNT", $_SESSION) && $_SESSION["ACCOUNT"] == $target){
    // do nothing, we are a-ok
}

In the above code (I think) we're checking to see if a session has already been set and exists for the present account. If so we do nothing. Otherwise we set the session in another else statement after this.

$_SESSION["ACCOUNT"] is being set in a cookie. The value "ACCOUNT" is the subdomain which is also used to identify the account in the database. Here are the lines from the cookie which shows the account is set. The account does exist.

SESSION[ACCOUNTID] = 39
SESSION[ACCOUNT] = demo
SESSION[PAIDACCOUNT] = 0

My question is how should that line of php now be coded so as not to throw that error?

Thanks!

Raptor
  • 53,206
  • 45
  • 230
  • 366
perdrix
  • 23
  • 1
  • 3

1 Answers1

2

You should use isset instead

else if(isset($_SESSION["ACCOUNT"]) && $_SESSION["ACCOUNT"] == $target){
    // do nothing, we are a-ok
}
BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
  • 1
    I'm not certain why this worked and mine didn't, but the error messages went away. Is this a change from php 4 to php 5.2.17? – perdrix Jan 16 '15 at 04:31
  • I'm not sure about what changed between PHP 4 and 5.2.17, but this works because `array_key_exists` throws an error if $_SESSION doesn't exist yet, where as `isset` just returns FALSE. – BrokenBinary Jan 16 '15 at 17:54
  • Thanks BrokenBinary for your assistance! – perdrix Jan 16 '15 at 18:33