检查 PHP cookie 是否存在,如果不存在,则设置其值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10738593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:55:04  来源:igfitidea点击:

Check if a PHP cookie exists and if not set its value

phpcookiessetcookie

提问by Teodor

I am working on a multilingual site so I tried this approach:

我在一个多语言网站上工作,所以我尝试了这种方法:

echo $_COOKIE["lg"];
if (!isset($_COOKIE["lg"]))
    setcookie("lg", "ro");
echo $_COOKIE["lg"];

The idea is that if the client doesn't have an lgcookie (it is, therefore, the first time they've visited this site) then set a cookie lg = rofor that user.

这个想法是,如果客户端没有lgcookie(因此,这是他们第一次访问此站点),lg = ro则为该用户设置一个 cookie 。

Everything works fine except that if I enter this page for the first time, the first and second echoreturn nothing. Only if I refresh the page is the cookie set and then both echoprint the "ro" string I am expecting.

一切正常,只是如果我第一次进入这个页面,第一次和第二次echo什么都不返回。只有当我刷新页面时才会设置 cookie,然后两者都echo打印我期望的“ro”字符串。

How can I set this cookie in order to see its value from the second echoon the first visit/page load of the user? Should be without needing to refresh the page or create a redirect.

如何设置此 cookie 以便echo在用户第一次访问/页面加载时查看第二个 cookie 的值?应该不需要刷新页面或创建重定向。

回答by Treffynnon

Answer

回答

You can't according to the PHP manual:

你不能根据PHP 手册

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

一旦设置了 cookie,就可以在下一页加载时使用 $_COOKIE 或 $HTTP_COOKIE_VARS 数组访问它们。

This is because cookies are sent in response headers to the browser and the browser must then send them back with the next request. This is why they are only available on the second page load.

这是因为 cookie 在响应标头中发送到浏览器,然后浏览器必须将它们与下一个请求一起发送回去。这就是它们仅在第二个页面加载时可用的原因。

Work around

解决

But you can work around it by also setting $_COOKIEwhen you call setcookie():

但是您也可以通过$_COOKIE在调用时进行设置来解决它setcookie()

if(!isset($_COOKIE['lg'])) {
    setcookie('lg', 'ro');
    $_COOKIE['lg'] = 'ro';
}
echo $_COOKIE['lg'];

回答by 0b10011

Cookies are onlysent at the time of the request, and therefore cannot be retrieved as soon as it is assigned (only available after reloading).

Cookie在请求时发送,因此无法在分配后立即检索(仅在重新加载后可用)。

Once the cookies have been set, they can be accessed on the next page loadwith the $_COOKIE or $HTTP_COOKIE_VARS arrays.

If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does notindicate whether the user accepted the cookie.

Cookies will not become visible until the next loading of a pagethat the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.

一旦设置了 cookie,就可以在下一页加载时使用 $_COOKIE 或 $HTTP_COOKIE_VARS 数组访问它们。

如果在调用此函数之前存在输出,则 setcookie() 将失败并返回 FALSE。如果 setcookie()成功运行,它将返回 TRUE。这没有指示用户是否接受该cookie

直到下一次加载cookie 应该可见的页面时,cookie 才会变得可见。要测试 cookie 是否已成功设置,请在 cookie 过期之前在下一个加载页面上检查 cookie。过期时间通过 expire 参数设置。调试 cookie 存在的一个好方法是简单地调用 print_r($_COOKIE);。

Source

来源