PHP的setcookie()有时是否需要一个值? [英] Does PHP's setcookie() sometimes require a value?

查看:68
本文介绍了PHP的setcookie()有时是否需要一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在使用以下代码来检测是否启用了cookie,但是最近发现,即使将浏览器设置为允许cookie,也无法成功。这是对是否依赖Cookie进行处理的简单检查。

I have been using the following code for a while to detect whether or not cookies are enabled but it was just recently discovered that even with browsers set to allow for cookies it wasn't succeeding. It is a simple check on whether or not to rely on cookies in the processing.

我们最近迁移到PHP版本5.6.21,但当前 setCookie()文档没说什么有关更改setcookie方法的信息,但要明确指出,如果您未指定任何内容,则默认为空白值。 PHP版本5.6.10也带来了不同的行为。

We recently migrated to PHP version 5.6.21 but the current setCookie() documentation doesn't say anything about changing the setcookie approach but makes it clear that if you don't specify anything a blank value is the default. We also get different behavior with PHP version 5.6.10.

这里是另一个问题谈到了类似的问题,但没有提及所使用的PHP版本。

Here's another question speaking to this similar issue but does not mention the PHP version being used.

值得纪念的是,所有设置cookie的示例都传递一个值(除了删除部分),以下是有关传递空白的说明:

To the documentation's credit, all the examples of setting cookies passes a value (except the deletion part) and here is a note about passing blank:


如果value参数为空字符串或FALSE,则所有其他参数匹配先前对setcookie的调用,然后将从远程客户端删除具有指定名称的cookie [强调我]

If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client [emphasis mine]

我对此的解释是,如果cookie 不存在(之前没有使用该名称的setcookie调用),它应该创建它

My interpretation of that is that if the cookie does not exist (no prior call to setcookie with that name) it should create it.

代码:

# Before any html is output:
function cookiesEnabled(){
    if(isset($_COOKIE["cookiesEnabled"])){
        return true;
    }else{
        if(isset($_GET["reloaded"])){
            return false;
        }else{
            setcookie("cookiesEnabled" /*,"ANY_VALUE_FIXES"*/);
            $query = "";
            if($_SERVER["QUERY_STRING"]){
                $query = "&".$_SERVER["QUERY_STRING"];
            }
            header("Location: ?reloaded=1".$query);
            exit();
        }
    }
}
# Check prior to any HTML content being echoed
if(!cookiesEnabled()){
    echo "Problem";
} else {
    echo "Success";
}

成功仅在值时显示参数。

我很满意填充一个值作为 fix /替代方法,我很高兴它似乎可以工作(并且很容易),但是这个问题是为了了解为什么行为似乎与文档不同,并希望从其他人那里得到确认

I am content populating a value as a "fix" / "workaround" and I'm glad it seems to work (and is easy) but this question is posed to learn about why the behavior seems to differ from the documentation and hopefully to gain confirmation from someone else.

我们的PHP版本要求值为5.6.21。我不确定其他哪些服务器设置可能会导致此问题。在装有PHP 5.6.10的其他服务器中,成功不需要值参数 。当我们刚刚将其版本升级到5.6.21时,它也失败了。

Our PHP version requiring a value is 5.6.21. I'm not sure what other server settings could be causing this behavior. In a different server with PHP 5.6.10, the value parameter is not required for "Success". When we just upgraded its version to 5.6.21, it also failed.

PHP是否需要更新其文档以明确表明需要一个值或引入了编码错误? ?我也没有任何联系人可以解决,因此感谢您将此潜在的差异转达给您。

Does PHP need to update their documentation to explicitly state a value is required or was a coding bug introduced? I don't have any contacts to address either so thanks for forwarding this potential discrepancy to the powers that be.

注意:如果我 setcookie 像这样的 setcookie( cookiesEnabled,),结果与不传递结果相同(在5.6.10中成功,在5.6中成功) .21),因此至少文档 可能默认将其设置为。

Note: If I setcookie like this setcookie("cookiesEnabled", ""), the result is the same as not passing it (Success in 5.6.10, not in 5.6.21) so at least the documentation probably is defaulting it to "".

推荐答案

我认为我将把它归因于版本差异和偶然发生的幕后编码更改。但是,仍然不清楚,根据PHP的文档,哪种方法是正确的。

I think I'll chalk it up to version differences and a behind-the-scenes coding change that I landed on accidentally. It still isn't clear, though, which way is correct based on PHP's documentation.

我相信PHP 5.6.21版本代表引入的错误和创建cookie。没有值(以前没有值)仍然可能。如果新行为是永久性的,则肯定需要更新文档。

I believe the PHP 5.6.21 version to represent an introduced bug and creating a cookie without a value (where there was none present before) should still be possible. If the new behavior is permanent, the documentation definitely needs to be updated.

据我所知,在PHP 5.6.10之前(包括PHP 5.6.10),不需要通过一个创建 cookie的值。

As far as I know, prior to and including PHP 5.6.10 it was not required to pass a value to create a new cookie.

如果更熟悉PHP内部工作原理的人可以说出的意图setcookie()而不会传递一个很好的值(并且该名称以前没有存在)。

If someone more intimate with PHP's inner workings can speak to the intention of setcookie() without passing a value (and no prior existence with that name) that would be great.

由于文档没有更改在这两个版本之间,其中一种方法是错误的,我希望这个问题可以解决问题。

Since the documentation didn't change between these two versions, one of the approaches is wrong and I'm hoping this question can get the ball rolling towards a fix.

如果您也遇到了这个问题,感谢您的评论。

If you have encountered this problem as well, thanks for chiming in by commenting.

这篇关于PHP的setcookie()有时是否需要一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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