php 命名 cookie - 最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2097843/
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
Naming cookies - best practices
提问by Emanuil Rusev
What should cookie names look like?
cookie 名称应该是什么样的?
Should they be:
他们应该是:
- lower_case
- CamelCase
- Underscore_Camel_Case
- UPPER_CASE
- 小写
- 骆驼香烟盒
- Underscore_Camel_Case
- 大写字母
Or should they be something else?
或者他们应该是别的东西?
采纳答案by Ignacio Vazquez-Abrams
appname_meaningfulname
appname_meaningfulname
回答by MatthieuP
Keep in mind that this cookie is sent with every request, so imho, just use the smallest name you can, and document your code nicely.
请记住,此 cookie 会随每个请求一起发送,所以恕我直言,请使用尽可能小的名称,并妥善记录您的代码。
回答by WillieMack
It should be something that avoids naming conflicts with arbitrary _GET and _POST params you might be using, since _REQUEST wraps all three global arrays (!), with precedence depending on how your variables_order setting is set in php.ini. In other words, if you have a _COOKIE named "x" and a querystring param named "x", and you ask for $_REQUEST["x"], you get the cookie value when you might want/expect the GET param. This is especially problematic if your cookies are scoped to your website root "/", and not to the folder where they are consumed.
它应该可以避免与您可能正在使用的任意 _GET 和 _POST 参数发生命名冲突,因为 _REQUEST 包装了所有三个全局数组 (!),其优先级取决于您在 php.ini 中的 variables_order 设置的设置方式。换句话说,如果您有一个名为“x”的 _COOKIE 和一个名为“x”的查询字符串参数,并且您要求 $_REQUEST["x"],那么当您可能需要/期望 GET 参数时,您将获得 cookie 值。如果您的 cookie 的范围是您的网站根目录“/”,而不是使用它们的文件夹,则这尤其成问题。
So I say, two best practices:
所以我说,两个最佳实践:
- make sure you limit scope of your cookies to the path where they are read and written, (third argument of setcookie() method does this)
- give your cookies some sort of cookie-specific naming convention. I suggest reverse website, like java namespaces, then ".".{appname}.".".{friendly cookie name camel cased} So, if your site is www.testsite.com, and your app is foo, and your variable is "bar bar bar bar bar barann", it would be "com.testsite.foo.barBarBarBarBarBarann"
- 确保将 cookie 的范围限制为读取和写入它们的路径,(setcookie() 方法的第三个参数执行此操作)
- 为您的 cookie 提供某种特定于 cookie 的命名约定。我建议反向网站,比如 java 命名空间,然后是 ".".{appname}.".".{friendly cookie name camel cased} 所以,如果你的网站是 www.testsite.com,你的应用是 foo,你的变量是“bar bar bar bar barann”,就是“com.testsite.foo.barBarBarBarBarBarann”
回答by zombat
I use whatever style the coding standards for the project call for.
我使用项目编码标准要求的任何风格。
Generally I prefer camelCase for naming schemes, but whichever one pays the bills is the one I'll go with.
一般来说,我更喜欢使用驼峰命名法,但无论谁付账,我都会选择。
回答by NineBerry
Maybe you won't like my answer:
也许你不会喜欢我的回答:
Don't use your own cookies but store data in server sessions. So you only need one cookie (to reference the session id) and how you name that plays no role.
不要使用您自己的 cookie,而是将数据存储在服务器会话中。因此,您只需要一个 cookie(用于引用会话 id)以及您如何命名它就没有任何作用。

