Shan
Shan

Reputation: 2832

How to check whether a page has been updated without downloading the entire webpage?

How to check whether a page has been updated without downloading the entire webpage in Php? Whether I need to look in at the header?

Upvotes: 1

Views: 446

Answers (2)

David Z.
David Z.

Reputation: 5701

One possibility is to check the LastModified header. You can download just the headers by issuing a HEAD request. The server responds back with the HTTP headers only and you can inspect the last modified header and/or the content length header to detect changes.

Last-modified "Mon, 03 Jan 2011 13:02:54 GMT"

One thing to note is that the HTTP server does not need to send this header so this would not work in all cases. The PHP function get_headers will fetch these for you.

// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);
$headers = get_headers('http://example.com');

Upvotes: 4

user1233508
user1233508

Reputation:

You can add a If-Modified-Since: <datetime> header to your request, and the server should return a 304 Not Modified if it hasn't changed since then. But if the document is generated dynamically (php, perl, etc.), the generator could be too lazy to check this header and always return the full document.

Upvotes: 2

Related Questions