gpt4 book ai didi

http-headers - 我可以使用 "http header"检查动态页面是否已更改

转载 作者:行者123 更新时间:2023-12-04 04:21:14 25 4
gpt4 key购买 nike

您可以请求 http header 通过查看网页的日期来检查网页是否已被编辑,但是诸如 - php、aspx- 从数据库中获取其数据的动态页面如何?

最佳答案

尽管您可能认为它已经过时,但我总是在 Conditional GET 上找到 Simon Willison 的文章。不仅仅是有用的。该示例使用 PHP,但它非常简单,您可以将其调整为其他语言。这是示例:

function doConditionalGet($timestamp) {
// A PHP implementation of conditional get, see
// http://fishbowl.pastiche.org/archives/001132.html
$last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
$etag = '"'.md5($last_modified).'"';

// Send the headers
header("Last-Modified: $last_modified");
header("ETag: $etag");

// See if the client has provided the required headers
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;

$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) :
false;

if (!$if_modified_since && !$if_none_match) {
return;
}

// At least one of the headers is there - check them
if ($if_none_match && $if_none_match != $etag) {
return; // etag is there but doesn't match
}

if ($if_modified_since && $if_modified_since != $last_modified) {
return; // if-modified-since is there but doesn't match
}

// Nothing has changed since their last request - serve a 304 and exit
header('HTTP/1.0 304 Not Modified');
exit;
}

有了这个,你可以使用 HTTP 动词 GETHEAD (我认为 others 也是可能的,但我看不出使用它们的原因)。您需要做的就是添加 If-Modified-SinceIf-None-Match具有相应的 header 值 Last-ModifiedETag由页面的先前版本发送。从 HTTP 1.1 版开始,建议使用 ETagLast-Modified ,但两者都可以完成工作。

这是一个非常简单的示例,说明条件 GET 的工作原理。首先,我们需要以通常的方式检索页面:

GET/some-page.html HTTP/1.1
主机:example.org

带有条件 header 和内容的第一个响应:

200 正常
ETag:您的ETag此处

现在有条件的获取请求:

GET/some-page.html HTTP/1.1
主机:example.org
If-None-Match: YourETagHere

并且响应指示您可以使用页面的缓存版本,因为只有标题将被传送:

304 未修改
ETag:您的ETag此处

有了这个,服务器通知您没有对页面进行修改。

我还可以向您推荐另一篇关于条件 GET 的文章: HTTP conditional GET for RSS hackers .

关于http-headers - 我可以使用 "http header"检查动态页面是否已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/204886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com