HttpCaching Class Reference

Class to make HTTP caching easier with PHP. More...

List of all members.

Public Member Functions

 sendStatusAndHeaders ($die)
 Send HTTP caching headers (Expires, Cache-Control, ETags and Last-Modified), and returns a 304 if the client has a cached version.
 sendHeaders ()
 Send HTTP caching headers (Expires, Cache-Control, ETags and Last-Modified).
 isFresh ()
 Determines whether the client has a fresh representation of the resource.
 etagMatch ($etag)
 Compares an etag with the resource's entity tag.
 getDuration ($type)
 Get the max-age or s-maxage cache control directive value.
 setDuration ($type, $time)
 Set max-age or s-maxage cache control directive value.
 freshFor ($time)
 Set max-age and the Expires header value.
 setCacheControlDirective ($type, $set)
 Set/unset Cache-Control directive.
 ccPublic ($set)
 Set/Unset Cache-Control public.
 ccNoCache ($set)
 Set/Unset Cache-Control no-cache.
 ccNoStore ($set)
 Set/Unset Cache-Control no-store.
 ccMustRevalidate ($set)
 Set/Unset Cache-Control must-revalidate.
 ccProxyRevalidate ($set)
 Set/Unset Cache-Control proxy-revalidate.
 etag ($value)
 Set value of the ETag header.
 weakEtag ($value)
 Set value of the ETag header to a weak value.
 lastModified ($value)
 Set value of the Last-Modified header.
 setLastModifiedFromFile ($file)
 Set value of the Last-Modified header using the last modification date of a file.

Static Public Member Functions

static isEtagWeak ($etag)
 Determines if an entity tag is weak.
static etagValidator ($etag)
 Returns the validator value of an entity tag.
static sendHeadersSpecifyingFreshness ($time)
 Send cache headers.
static formatDate ($time)
 Format a date in RFC 1123 date format.


Detailed Description

Class to make HTTP caching easier with PHP.

Home page: http://larve.net/2006/08/php-http-caching/

Version:
0.5
Author:
Hugo Haas - http://larve.net/people/hugo/

Contributions from Kai Bolay

Examples:

demo.php, one-liner.php, simple-etag.php, and simple.php.

Definition at line 13 of file HttpCaching.php.


Member Function Documentation

HttpCaching::ccMustRevalidate ( set  ) 

Set/Unset Cache-Control must-revalidate.

Parameters:
$set true or false to set or unset the parameter

Definition at line 297 of file HttpCaching.php.

References setCacheControlDirective().

HttpCaching::ccNoCache ( set  ) 

Set/Unset Cache-Control no-cache.

Parameters:
$set true or false to set or unset the parameter

Definition at line 281 of file HttpCaching.php.

References setCacheControlDirective().

HttpCaching::ccNoStore ( set  ) 

Set/Unset Cache-Control no-store.

Parameters:
$set true or false to set or unset the parameter

Definition at line 289 of file HttpCaching.php.

References setCacheControlDirective().

HttpCaching::ccProxyRevalidate ( set  ) 

Set/Unset Cache-Control proxy-revalidate.

Parameters:
$set true or false to set or unset the parameter

Definition at line 305 of file HttpCaching.php.

References setCacheControlDirective().

HttpCaching::ccPublic ( set  ) 

Set/Unset Cache-Control public.

Parameters:
$set true or false to set or unset the parameter

Definition at line 273 of file HttpCaching.php.

References setCacheControlDirective().

HttpCaching::etag ( value  ) 

Set value of the ETag header.

Parameters:
$value Value (string)
The string will be quoted automatically.

Definition at line 315 of file HttpCaching.php.

Referenced by weakEtag().

HttpCaching::etagMatch ( etag  ) 

Compares an etag with the resource's entity tag.

Parameters:
$etag Entity tag to compare
Returns:
true if they match, false if they don't

Definition at line 157 of file HttpCaching.php.

Referenced by isFresh().

static HttpCaching::etagValidator ( etag  )  [static]

Returns the validator value of an entity tag.

Parameters:
$etag Entity tag
Returns:
The validator value ($etag if the entity tag is strong, or what's after 'W/' otherwise)

Definition at line 190 of file HttpCaching.php.

static HttpCaching::formatDate ( time  )  [static]

Format a date in RFC 1123 date format.

Parameters:
$time Time since epoch in seconds
Returns:
Formatted date (e.g. "Sun, 27 Aug 2006 08:32:28 GMT")
Examples:
demo.php.

Definition at line 369 of file HttpCaching.php.

HttpCaching::freshFor ( time  ) 

Set max-age and the Expires header value.

Parameters:
$time Duration as a positive number of seconds, a string for strtotime(), or a negative number to disable this directive
Returns:
Updated value
Exceptions:
Throws an exception for invalid values of $time

Definition at line 249 of file HttpCaching.php.

References setDuration().

HttpCaching::getDuration ( type  ) 

Get the max-age or s-maxage cache control directive value.

Parameters:
$type "max-age" or "s-maxage"
Returns:
The value in seconds; if -1 is returned, no max-age directive will be sent
Exceptions:
Throws an exception for invalid values of $type

Definition at line 205 of file HttpCaching.php.

static HttpCaching::isEtagWeak ( etag  )  [static]

Determines if an entity tag is weak.

Parameters:
$etag Entity tag
Returns:
true if the entity tag is weak, false otherwise

Definition at line 180 of file HttpCaching.php.

HttpCaching::isFresh (  ) 

Determines whether the client has a fresh representation of the resource.

Returns:
true if the version the client has is fresh, false if this is not the case and a new version needs to be returned
This function compares the If-Modified-Since and If-None-Match headers provided by the client to the values specified for this resource, and returns true if the resource has been modified or false if the client's version is current.

Definition at line 112 of file HttpCaching.php.

References etagMatch(), and lastModified().

Referenced by sendStatusAndHeaders().

HttpCaching::lastModified ( value  ) 

Set value of the Last-Modified header.

Parameters:
$value Unix timestamp

Definition at line 334 of file HttpCaching.php.

Referenced by isFresh(), sendHeaders(), and setLastModifiedFromFile().

HttpCaching::sendHeaders (  ) 

Send HTTP caching headers (Expires, Cache-Control, ETags and Last-Modified).

Expires and Cache-Control are sent as set.

ETag is sent if set. Last-Modified is sent if set or if ETag isn't set, defaulting to the current time. Indeed, at least you of the two needs to be present for the response to be cacheable.

Definition at line 69 of file HttpCaching.php.

References lastModified().

Referenced by sendStatusAndHeaders().

static HttpCaching::sendHeadersSpecifyingFreshness ( time  )  [static]

Send cache headers.

Parameters:
$time Freshness duration
This is for a simple scenario: specifying freshness, and must-revalidate.
Examples:
one-liner.php.

Definition at line 357 of file HttpCaching.php.

HttpCaching::sendStatusAndHeaders ( die  ) 

Send HTTP caching headers (Expires, Cache-Control, ETags and Last-Modified), and returns a 304 if the client has a cached version.

Parameters:
$die if set to true, the program terminates if a 304 is being sent, and if set to false, the call will return and the execution will continue
Returns:
false if a 304 was returned, true otherwise
This function returns a 304 if the client already has the latest version of the resource's representation (see HttpCaching::isFresh). The PHP script will subsequently quit if $die is set to true.

The headers are sent with HttpCaching::sendHeaders.

Definition at line 44 of file HttpCaching.php.

References isFresh(), and sendHeaders().

HttpCaching::setCacheControlDirective ( type,
set 
)

Set/unset Cache-Control directive.

Parameters:
$type Cache-Control directive (e.g. "public")
$set true or false to set or unset the parameter

Definition at line 258 of file HttpCaching.php.

Referenced by ccMustRevalidate(), ccNoCache(), ccNoStore(), ccProxyRevalidate(), and ccPublic().

HttpCaching::setDuration ( type,
time 
)

Set max-age or s-maxage cache control directive value.

Parameters:
$type "max-age" or "s-maxage"
$time Duration as a positive number of seconds, a string for strtotime(), or a negative number to disable this directive
Returns:
Updated value
Exceptions:
Throws an exception for invalid values of $time or $type

Definition at line 220 of file HttpCaching.php.

Referenced by freshFor().

HttpCaching::setLastModifiedFromFile ( file  ) 

Set value of the Last-Modified header using the last modification date of a file.

Parameters:
$file File whose modification time will be used

Definition at line 343 of file HttpCaching.php.

References lastModified().

HttpCaching::weakEtag ( value  ) 

Set value of the ETag header to a weak value.

Parameters:
$value Value (string)
The string will be quoted automatically.

Definition at line 325 of file HttpCaching.php.

References etag().


The documentation for this class was generated from the following file:
PHP HTTP Caching
Hugo Haas