0

I haven't made any changes to the code affecting the COOKIE's and now I get the following:

Use of undefined constant COOKIE_LOGIN - assumed 'COOKIE_LOGIN'

//Destroy Cookie
if (isset($_COOKIE[COOKIE_LOGIN]) && !empty($_COOKIE[COOKIE_LOGIN]))
    setcookie(COOKIE_LOGIN,$objUserSerialized,time() - 86400 );

I'm not sure what I need to do to actually change this since I do not know what chnaged to begin with and so cannot track the problem.

Thanks.

AFG
  • 1,675
  • 3
  • 22
  • 23
  • See also [my attempt at a canonical answer for causes of this error message](http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean/8025500#8025500). – John Carter Nov 06 '11 at 06:50

2 Answers2

8

You need to surround the array key by quotes:

if (isset($_COOKIE['COOKIE_LOGIN']) && !empty($_COOKIE['COOKIE_LOGIN']))
    setcookie('COOKIE_LOGIN',$objUserSerialized,time() - 86400 );

PHP converts unknown literals to strings and throws a warning. Your php.ini probably had the error reporting level to not display warnings but someone may have updated it or something. In either case, it is bad practice to take advantange of PHP's behavior in this case.

For more information, check out the php documentation:

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.

Community
  • 1
  • 1
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
1

You can't say $_COOKIE[COOKIE_LOGIN] without error unless COOKIE_LOGIN is an actual constant that you have defined, e.g.:

define("COOKIE_LOGIN", 5);

Some people have habits where they will write code like:

$ary[example] = 5;
echo $ary[example];

Assuming that "example" (as a string) will be the array key. PHP has in the past excused this behavior, if you disable error reporting. It's wrong, though. You should be using $_COOKIE["COOKIE_LOGIN"] unless you have explicitly defined COOKIE_LOGIN as a constant.

Peter
  • 1,316
  • 8
  • 4