====== Controllo cache pagine web ====== ===== Header HTTP ===== Il metodo più corretto per controllare il meccanismo di caching è tramite gli header HTTP, quindi è possibile farlo con pagine generate dinamicamente ad esempio in PHP. Primo esempio di pagina che può essere tenuta in cache (ad esempio dal browser) per 5 minuti: header("Pragma: cache"); header("Cache-Control: max-age=300, must-revalidate"); ===== Direttive HTML ===== Oltre alle indicazioni trasmesse tramite header è possibile inserire indicazioni sul caching anche dentro il documento HTML con opportuni tag ''****''. GMT"> ===== Cache-Control ===== Questo è il metodo corretto da usare in HTTP/1.1 per gestire il caching delle pagine web. ^ public | Indicates that the response MAY be cached by any cache, even if it would normally be non-cacheable or cacheable only within a non-shared cache. | ^ private | Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. A private (non-shared) cache MAY cache the response. | ^ no-cache | If the no-cache directive does not specify a field-name, then a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests. | ^ must-revalidate | When the must-revalidate directive is present in a response received by a cache, that cache MUST NOT use the entry after it becomes stale to respond to a subsequent request without first revalidating it with the origin server. | ^ max-age= | The expiration time of an entity MAY be specified by the origin server using the Expires header. Alternatively, it MAY be specified using the max-age directive in a response. If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive. | ===== Pragma: no-cache ===== Con HTTP/1.0 questo è l'unico controllo per gestire il caching delle pagine web. The directive ''**Pragma: no-cache**'' indicates cached information should not be used and instead requests should be forwarded to the origin server. This directive has the same semantics as the ''Cache-Control: no-cache'' directive and is provided for backwards compatibility with HTTP/1.0. Clients SHOULD include both ''Pragma: no-cache'' and ''Cache-Control: no-cache'' when a no-cache request is sent to a server not known to be HTTP/1.1 compliant. HTTP/1.1 clients SHOULD NOT send the PRAGMA request-header. HTTP/1.1 caches SHOULD treat ''Pragma: no-cache'' as if the client had sent ''Cache-Control: no-cache''. Ecco come includere la direttiva con il PHP oppure direttamente dentro il codice HTML: header("Pragma: no-cache"); La direttiva ''**Pragma: cache**'' pare non essere documentata, ma **Microsoft Explorer** la utilizza almeno in un caso: su connessione **https**, il download di un documento pdf generato al volo viene visualizzato inline **solo se** c'è la direttiva ''Pragma: cache''. ===== HTTPS, caching, file generati al volo ===== Attenzione alla gestione del caching delle pagine quando si usa https, in generale la tendenza dei browser pare essere quella di non utilizzare la cache per pagine ricevute via https. Con **Microsoft Internet Explorer** il download di un file generato al volo (ad esempio un PDF generato tramite PHP) potrebbe fallire se non viene dichiarato con gli opportuni header la possibilità di fare cache. Sempre con Expolorer la mancanza della direttiva ''Pragma: cache'' impedisce la visualizzazione inline, provocando la comparsa del box //Apri//, //Salva//, ecc. Con **Firefox** non si riesce ad impedire il reload di una pagina ricevuta via https quando si preme il pulsante "back" del browser.