为您的 cookie 提供某种特定于 cookie 的命名约定。我建议反向网站,例如java命名空间,然后“.”.{appname}.“.”.{友好的cookie名称驼峰命名}所以,如果您的网站是www.testsite.com,您的应用程序是foo,并且您的变量是“bar bar bar bar barann”,它将是“com.testsite.foo.barBarBarBarBarBarann”
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.
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"
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.
发布评论
评论(5)
appname_meaningfulname
appname_meaningfulname
请记住,这个 cookie 是随每个请求一起发送的,所以恕我直言,只需使用尽可能小的名称,并很好地记录您的代码。
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.
它应该避免与您可能使用的任意 _GET 和 _POST 参数发生命名冲突,因为 _REQUEST 包装了所有三个全局数组(!),其优先级取决于您在 php.ini 中设置的 Variables_order 设置。换句话说,如果您有一个名为“x”的 _COOKIE 和一个名为“x”的查询字符串参数,并且您请求 $_REQUEST["x"],则当您可能需要/期望 GET 参数时,您会获得 cookie 值。如果您的 Cookie 的范围仅限于您的网站根目录“/”,而不是使用它们的文件夹,则这尤其成问题。
所以我说,有两个最佳实践:
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.
So I say, two best practices:
我使用项目编码标准要求的任何风格。
一般来说,我更喜欢使用驼峰式命名方案,但无论哪种方案都能满足我的需求,我都会选择哪种方案。
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.
也许你不喜欢我的回答:
不要使用你自己的cookie,而是将数据存储在服务器会话中。因此,您只需要一个 cookie(用于引用会话 ID),而您的命名方式则无关紧要。
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.