PHP可靠地设置Etag
我无法在用户浏览器上可靠地设置 Etag。当用户单击我的外部链接之一时,我想将文章 id 设置到他们的 Etag 中(我也使用 cookie,但 id 喜欢专门尝试 Etag 以测试其可靠性)。
当同一用户在几个小时/几天后返回我的网站时,我希望能够读取 Etag 值并将其用于其他用途。
我可以在初始点击时设置 Etag,但是当用户返回时,Etag 值就消失了。我猜它已经过期了或者什么的。这是我一直在尝试的代码:
<?
$time = 1280951171;
$lastmod = gmdate('D, d M Y H:i:s \G\M\T', $time);
$etag = '123';
$ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod : null;
$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : null;
if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) {
header('HTTP/1.0 304 Not Modified');
} else {
header("Last-Modified: $lastmod");
header("ETag: $etag");
}
print_r($_SERVER);
?>
im having trouble setting the Etag on a user's browser reliably. When a user clicks on one of my external links, i would like to set the article id into their Etag (i use cookies too, but id like to experiment with Etag specifically to test its reliability).
When the same user returns to my site a few hours/days later, i would like to be able to read the Etag value and use it for stuff.
I can set an Etag on the initial click, but when the user returns the Etag value is gone. I assume its expired or something. Here is the code i have been trying:
<?
$time = 1280951171;
$lastmod = gmdate('D, d M Y H:i:s \G\M\T', $time);
$etag = '123';
$ifmod = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $lastmod : null;
$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : null;
if (($ifmod || $iftag) && ($ifmod !== false && $iftag !== false)) {
header('HTTP/1.0 304 Not Modified');
} else {
header("Last-Modified: $lastmod");
header("ETag: $etag");
}
print_r($_SERVER);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将您的 etag 用双引号括起来(如 Codler 提到的链接所示):
我认为这不太可能解决您的问题,但您可能希望
从
PHP 5.4 开始,有一种更好的方法可以使用
http_response_code
:另外,您检查过吗对于常见的嫌疑人停止标头? Unicode 字节顺序标记对此非常烦人。 (如果您可以看到您自己设置的其他标题,请忽略)
You should be wrapping your etag in double quotes (as the link Codler mentioned shows):
I don't think it's likely to solve your problem, but you probably want
instead of
As of PHP 5.4 there's a better way to do this with
http_response_code
:Also, have you checked for the usual suspects stopping headers? Unicode Byte-Order Markers are very annoying with this. (Disregard if you can see other headers you're setting yourself)
Etag不太可靠
http://developer.yahoo.com/performance/rules.html#etags
Etag is not so reliable
http://developer.yahoo.com/performance/rules.html#etags