Enable support for HTTP/1.x conditional requests in PHP.
Goal: Optimisation
- If the client sends a HEAD request, avoid transferring data and return the correct headers.
- If the client already has the same version in its cache, avoid transferring data again (304 Not Modified).
- Possibility to control cache for client and proxies (public or private policy, life time).
- When $feedMode is set to true, in the case of a RSS/ATOM feed,
it puts a timestamp in the global variable $clientCacheDate to allow the sending of only the articles newer than the client's cache.
- When $compression is set to true, compress the data before sending it to the client and persitent connections are allowed.
- When $session is set to true, automatically checks if $_SESSION has been modified during the last generation the document.
Interface:
- function httpConditional($UnixTimeStamp,$cacheSeconds=0,$cachePrivacy=0,$feedMode=false,$compression=false)
[Required] $UnixTimeStamp: Date of the last modification of the data to send to the client (Unix Timestamp format).
[Implied] $cacheSeconds=0: Lifetime in seconds of the document. If $cacheSeconds<0,cacheisdisabled.If$cacheSeconds==0,thedocumentwillberevalidatedeachtimeitisaccessed.If$cacheSeconds>0, the document will be cashed and not revalidated against the server for this delay.
[Implied] $cachePrivacy=0: 0=private, 1=normal (public), 2=forced public. When public, it allows a cashed document ($cacheSeconds>0) to be shared by several users.
[Implied] $feedMode=false: Special RSS/ATOM feeds. When true, it sets $cachePrivacy to 0 (private), does not use the modification time of the script itself, and puts the date of the client's cache (or a old date from 1980) in the global variable $clientCacheDate.
[implied] $compression=false: Enable the compression and allows persistant connections (automatic detection of the capacities of the client).
[implied] $session=false: To be turned on when sessions are used. Checks if the data contained in $_SESSION has been modified during the last generation the document.
Returns: True if the connection can be closed (e.g.: the client has already the lastest version), false if the new content has to be send to the client.
Typical use:
<?php
require_once('http-conditional.php');
//Date of the last modification of the content (Unix Timestamp format).
//Examples: query the database, or last modification of a static file.
$dateLastModification=...;
if (httpConditional($dateLastModification))
{
... //Close database connections, and other cleaning.
exit(); //No need to send anything
}
//Do not send any text to the client before this line.
... //Rest of the script, just as you would do normally.