我可以使用“http标头”吗?检查动态页面是否已更改 [英] can i use "http header" to check if a dynamic page has been changed

查看:163
本文介绍了我可以使用“http标头”吗?检查动态页面是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

you can request the http header to check if a web page has been edited by looking at its date but how about dynamic pages such as - php, aspx- which grabs its data from a database?

推荐答案

即使你认为它已经过时了我总是找到Simon Willison关于条件GET 不仅仅是有用的。该示例在PHP中,但它非常简单,您可以将其适用于其他语言。这是一个例子:

Even though you might think it's outdated I've always found Simon Willison's article on Conditional GET to be more than useful. The example is in PHP but it is so simple that you can adapt it to other languages. Here it is the example:

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动词 GET HEAD (我认为这也可以用其他,但我看不出使用它们的原因)。您需要做的就是添加 If-Modified-Since If-None-Match 以及相应的值标题 Last-Modified ETag 由该页面的先前版本发送。从HTTP版本1.1开始,建议 ETag 超过 Last-Modified ,但两者都可以完成工作。

With this you can use HTTP verbs GET or HEAD (I think it's also possible with the others, but I can't see the reason to use them). All you need to do is adding either If-Modified-Since or If-None-Match with the respective values of headers Last-Modified or ETag sent by a previous version of the page. As of HTTP version 1.1 it's recommended ETag over Last-Modified, but both will do the work.

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

This is a very simple example of how a conditional GET works. First we need to retrieve the page the usual way:

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

首先回应条件标题和内容:

First response with conditional headers and contents:

200 OK
ETag: YourETagHere

现在条件获取请求:

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

并且响应表明您可以使用缓存该页面的版本,因为只有标题将被传递:

And the response indicating you can use the cached version of the page, as only the headers are going to be delivered:

304 Not Modified
ETag: YourETagHere

有了这个,服务器通知你没有修改页面。

With this the server notified you there was no modification to the page.

我还可以推荐你另一篇关于条件GET的文章: RSS黑客的HTTP条件GET

I can also recommend you another article about conditional GET: HTTP conditional GET for RSS hackers.

这篇关于我可以使用“http标头”吗?检查动态页面是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆