Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understand If-Modified-Since HTTP Header

I am looking at a Caching library that is trying to use the If-Modified-Since header of a request object. The problem is this header never gets set, it is always blank which makes sense to me seeing how it is a REQUEST.

How can you force a request to have a If-Modified-Since header? Or am I way off for what this does.

Here is the function I am referring to.

public function isNotModified(Request $request)
{
    $lastModified = $request->headers->get('If-Modified-Since');

    $notModified = false;
    if ($etags = $request->getEtags()) {
        $notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
    } elseif ($lastModified) {
        $notModified = $lastModified == $this->headers->get('Last-Modified');
    }

    if ($notModified) {
        $this->setNotModified();
    }

    return $notModified;
}
like image 362
Mike Avatar asked Feb 22 '11 17:02

Mike


People also ask

What does if-modified-since mean in HTTP?

The If-Modified-Since request HTTP header makes the request conditional: the server sends back the requested resource, with a 200 status, only if it has been last modified after the given date.

Do you see an if-modified-since line in the HTTP GET?

Now inspect the contents of the second HTTP GET request from your browser to the server. Do you see an “IF-MODIFIED-SINCE:” line in the HTTP GET? If so, what information follows the “IF-MODIFIED-SINCE:” header? Answer: Yes.

What are the If-modified-since and if-none-match header used for?

The If-Modified-Since header is used to specify the time at which the browser last received the requested resource. The If-None-Match header is used to specify the entity tag that the server issued with the requested resource when it was last received.

What HTTP response header tag indicates when the request resource was modified last?

The Last-Modified response HTTP header contains a date and time when the origin server believes the resource was last modified. It is used as a validator to determine if the resource is the same as the previously stored one. Less accurate than an ETag header, it is a fallback mechanism.


2 Answers

A request with If-Modified-Since only makes sense if the client already has a resource which is obtained along with a response that has a Last-Modified header in combination with headers which allow browser caching like a Cache-Control and/or Pragma value containing public.

Also, I've noticed that some browsers does not include If-Modified-Since when the original response also contained an ETag header. The browser will instead use If-None-Match to test it.

See also:

  • Caching tutorial for web authors and web masters
like image 131
BalusC Avatar answered Sep 29 '22 23:09

BalusC


First you have to make sure the initial response is cached in the first place (I answered this in another, related question.

Try to set the following fields:

Last-Modified: Wed, 16 Feb 2011 13:52:26 GMT
Expires: -1
Cache-Control: must-revalidate, private
  • Last-Modified is needed as a validator (do not send ETag if you want to test for If-Modified-Since)
  • Expires -1 tells that the resource is stale and must always be revalidated
  • Cache-Control must not include no-cache nor no-store

When you send these headers on the initial HTTP/200 response, on subsequent requests, the browser should send conditional requests that include the If-Modified-Since header.

like image 44
alienhard Avatar answered Oct 02 '22 23:10

alienhard