如何告诉浏览器不要为某些文件夹、文件或请求发送 COOKIE?

发布于 2024-08-16 15:25:57 字数 300 浏览 5 评论 0原文

我正在运行 PHP5 和 Apache,并且启用了 mod_rewrite。

我们知道在每个页面加载时浏览器都会将 cookie 数据发送到请求的文件。这些数据对于 PHP 文件可能有用,但对于图像或 css 文件来说它没有任何价值,只会使浏览器和服务器之间的通信变慢。

有什么方法可以告诉浏览器或服务器停止对某些文件类型或目录执行此操作?

I'm running PHP5 and Apache with mod_rewrite enabled.

As we know on every page load browser sends cookie data to requested files. This data could be useful for PHP files, however for images or css files it has no value and simply make communication between browser and server slower.

Is there any ways to tell browser or server to stop doing so for certain file types or directories?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-08-23 15:25:57

有两种方法:

  1. 将 cookie 的路径限制为仅包含需要 cookie 的 php 脚本的站点部分,或者
  2. 从不同的域提供图像和 css,在该域中不会发送 cookie。

Two ways:

  1. Restrict the path of the cookie to only the part of your site containing php scripts that need the cookies, or
  2. Serve images and css from a different domain, where the cookies won't get sent.
再可℃爱ぅ一点好了 2024-08-23 15:25:57

我查找了 cookie 规范

当用户代理向源服务器发送请求时,如果它存储了适用于该请求的 Cookie,则它会包含 Cookie 请求标头。

它描述的唯一条件是域、路径、端口和安全性 (https)。如果您无法重组您的应用程序来避免这种情况,您将获得无关的 cookie,并且您可能对此无能为力(除非更换 Internet 上的所有浏览器,或者只是从一开始就不发送 cookie) )。我会考虑在 / 处放置一个重定向,将其指向一个子目录,但这可能与您希望节省的开销一样多,并且具有语义含义。

你对它进行了基准测试吗?这到底有多大意义?您要发送多大的 cookie?您还可以进行其他优化来改善用户体验吗?

Cookie语法,参考:

   set-cookie      =       "Set-Cookie2:" cookies
   cookies         =       1#cookie
   cookie          =       NAME "=" VALUE *(";" set-cookie-av)
   NAME            =       attr
   VALUE           =       value
   set-cookie-av   =       "Comment" "=" value
                   |       "CommentURL" "=" <"> http_URL <">
                   |       "Discard"
                   |       "Domain" "=" value
                   |       "Max-Age" "=" value
                   |       "Path" "=" value
                   |       "Port" [ "=" <"> portlist <"> ]
                   |       "Secure"
                   |       "Version" "=" 1*DIGIT
   portlist        =       1#portnum
   portnum         =       1*DIGIT

I looked up the cookie specification.

When it sends a request to an origin server, the user agent includes a Cookie request header if it has stored cookies that are applicable to the request.

The only conditions that it describes are domain, path, port, and security (https). If you can't restructure your application to avoid this, you're going to get the extraneous cookies, and there's probably nothing you can do about it (short of replacing all the browsers on the Internet, or just not sending cookies to begin with). I'd consider putting a redirect at / to point it to a subdirectory, but that's probably just as much overhead as you're hoping to save, and has semantic implications.

Have you benchmarked it? How big a deal is it exactly? How big of cookies are you sending? Are there other optimizations you could be doing to improve your user experience instead?

Cookie syntax, for reference:

   set-cookie      =       "Set-Cookie2:" cookies
   cookies         =       1#cookie
   cookie          =       NAME "=" VALUE *(";" set-cookie-av)
   NAME            =       attr
   VALUE           =       value
   set-cookie-av   =       "Comment" "=" value
                   |       "CommentURL" "=" <"> http_URL <">
                   |       "Discard"
                   |       "Domain" "=" value
                   |       "Max-Age" "=" value
                   |       "Path" "=" value
                   |       "Port" [ "=" <"> portlist <"> ]
                   |       "Secure"
                   |       "Version" "=" 1*DIGIT
   portlist        =       1#portnum
   portnum         =       1*DIGIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文