重复的Cookie [英] Duplicate cookies

查看:114
本文介绍了重复的Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置在PHP两个cookie在我的根目录




 的setcookie(ST ,$ todays_system_date); 
setcookie(en,$ tomorrow);


我的问题是我需要在脚本中重新设置是在一个更深的文件夹如:/ diary /
当我设置它们,然后尝试阅读他们或查看cookie集,我发现我有2x en和2x st饼干。



我的问题是,如何才能参考一组cookie?和阅读?



在此先感谢


解决方案

Cookies是关键/ value对 - 它们在任何给定范围内只能有一个值。



但是:




  • 对于同一个域,可能有多个Cookie具有相同的键和不同的值(尽管在任何给定的范围内它仍然只有一个有效值)。



例如,如果您在根目录中设置了cookie st code>, 1 / diary / 2 ,这两个cookie会存在在自己的权利。当您在 / 目录(或 / 的任何子目录,而不是 / diary / / diary / 的子目录),该值将为 1 。当你在 /日志/ 或子目录 /日志/ 如果将 2

 目录Cookie值
/ 1
/ somedir / 1
/日记/ 2
/日记/子目录2
/ somedir /日记/ 1





  • 可以在同一范围内设置相同的Cookie两次,但只有后一个值



例如:

  setcookie('st',1); // st现在为1为当前路径
setcookie('st',2); // st现在为2当前路径

如果查看发送的HTTP响应消息通过PHP,您将看到 st 的2 Set-Cookie:但是,在cookie范围内的下一个请求中,只有稍后的 2 会被发送回服务器。


I am setting two cookies in PHP in my root directory

   setcookie ("st", "$todays_system_date"  );
   setcookie ("en", "$tomorrow" );

My problem is I need to set them again in a script that is in a deeper folder eg: /diary/ When I do set them and then try to read them or look at the cookies set , I find I have 2x en and 2x st cookies .

My question is how can I make reference to just one set of cookies ? and read them ?

thanks in advance

解决方案

Cookies are key/value pairs - they will only ever have a single value in any given scope.

However:

  • It is possible to have more than one cookie with the same key and different values for the same domain (although it will still only have one effective value in any given scope).

For example, if you set the cookie st in the root (/) with the value 1, and in /diary/ with value 2, the two cookies will exist in their own right. When you are in the / directory (or any subdirectory of / that is not /diary/ or a subdirectory of /diary/) the value will be 1. When you are in /diary/ or a subdirectory of /diary/ if will be 2:

Directory         Cookie Value
/                      1
/somedir/              1
/diary/                2
/diary/subdir          2
/somedir/diary/        1


  • It is possible to set the same cookie twice in the same scope, but only the later value will be effective - the later value overrides the earlier value.

For example:

setcookie('st',1); // st is now 1 for the current path
setcookie('st',2); // st is now 2 for the current path

If you look at the HTTP response message that is sent by PHP you will see 2 Set-Cookie: headers for the key st. However, on the next request within the cookie's scope, only the later value 2 will be sent back to the server.

这篇关于重复的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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