PHP:获得“未定义常量COOKIE_LOGIN的使用”如何解决这个问题? [英] PHP: getting a "use of undefined constant COOKIE_LOGIN" how do I fix this?

查看:399
本文介绍了PHP:获得“未定义常量COOKIE_LOGIN的使用”如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有对影响COOKIE的代码做任何更改,现在我得到以下结果:



使用未定义的常量COOKIE_LOGIN - 假设COOKIE_LOGIN p>

  //销毁Cookie 
if(isset($ _ COOKIE [COOKIE_LOGIN])&&!empty($ _ COOKIE [ COOKIE_LOGIN]))
setcookie(COOKIE_LOGIN,$ objUserSerialized,time() - 86400);

我不知道我需要做什么来实际改变这个,因为我不知道什么chnaged

解决方案

p>您需要用引号将数组关键字括起来:

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

PHP将未知字面量转换为字符串并引发警告。您的php.ini可能有错误报告级别不显示警告,但有人可能已更新它或某事。在这两种情况下,在这种情况下利用PHP的行为是不好的做法。



有关更多信息,请查看 php文档


字符串字面数组索引。例如,$ foo ['bar']是正确的,而$ foo [bar]不是。



这是错误的,但它的工作原理。原因是这个代码有一个未定义的常量(bar),而不是一个字符串('bar' - 注意引号)。 PHP可能在未来定义常量,不幸的是这样的代码,有相同的名称。它的工作原理,因为PHP自动转换一个裸字符串(一个未引号的字符串,不对应任何已知的符号)到一个包含裸字符串的字符串。例如,如果没有定义的常量命名条,那么PHP将在字符串'bar'中替换并使用它。



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.

解决方案

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.

这篇关于PHP:获得“未定义常量COOKIE_LOGIN的使用”如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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