如何告诉浏览器不要为某些文件夹、文件或请求发送 COOKIE?
我正在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种方法:
Two ways:
我查找了 cookie 规范。
它描述的唯一条件是域、路径、端口和安全性 (https)。如果您无法重组您的应用程序来避免这种情况,您将获得无关的 cookie,并且您可能对此无能为力(除非更换 Internet 上的所有浏览器,或者只是从一开始就不发送 cookie) )。我会考虑在 / 处放置一个重定向,将其指向一个子目录,但这可能与您希望节省的开销一样多,并且具有语义含义。
你对它进行了基准测试吗?这到底有多大意义?您要发送多大的 cookie?您还可以进行其他优化来改善用户体验吗?
Cookie语法,参考:
I looked up the cookie specification.
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: