<?php require '../../config.php'; require 'HTTP/ConditionalGet.php'; // emulate regularly updating document $every = 20; $lastModified = round(time() / $every) * $every - $every; $cg = new HTTP_ConditionalGet(array('lastModifiedTime' => $lastModified)); $cg->sendHeaders(); if ($cg->cacheIsValid) { // we're done exit; } $title = 'Last-Modified is known : simple usage'; $explain = ' <p>If your content has not changed since a certain timestamp, set this via the the <code>lastModifiedTime</code> array key when instantiating HTTP_ConditionalGet. You can immediately call the method <code>sendHeaders()</code> to set the Last-Modified, ETag, and Cache-Control headers. The, if <code>cacheIsValid</code> property is false, you echo the content.</p> <p>This script emulates a document that changes every ' . $every . ' seconds. <br>This is version: ' . date('r', $lastModified) . '</p> '; require '_include.php'; echo send_slowly(get_content(array('title' => $title, 'explain' => $explain)));
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/../../min/lib')); require 'HTTP/ConditionalGet.php'; // generate content first (not ideal) // emulate regularly updating document $every = 20; $lastModified = round(time() / $every) * $every - $every; $title = 'Last-Modified is unknown : use hash of content for ETag'; $explain = ' <p>When Last-Modified is unknown, you can still use ETags, but you need a short string that is unique for that content. In the worst case, you have to generate all the content first, <em>then</em> instantiate HTTP_ConditionalGet, setting the array key <code>contentHash</code> to the output of a hash function of the content. Since we have the full content, we might as well also use <code>setContentLength(strlen($content))</code> in the case where we need to send it.</p> <p>This script emulates a document that changes every ' . $every . ' seconds. <br>This is version: ' . date('r', $lastModified) . '</p> '; require '_include.php'; $content = get_content(array('title' => $title, 'explain' => $explain)); $cg = new HTTP_ConditionalGet(array('contentHash' => substr(md5($content), 7))); if ($cg->cacheIsValid) { $cg->sendHeaders(); // we're done exit; } $cg->setContentLength(strlen($content)); $cg->sendHeaders(); send_slowly($content);
<?php require __DIR__ . '/../../bootstrap.php'; // emulate regularly updating document $every = 20; $lastModified = round(time() / $every) * $every - $every; list($enc, ) = HTTP_Encoder::getAcceptedEncoding(); $cg = new HTTP_ConditionalGet(array('lastModifiedTime' => $lastModified, 'encoding' => $enc)); $cg->sendHeaders(); if ($cg->cacheIsValid) { // we're done exit; } // output encoded content $title = 'ConditionalGet + Encoder'; $explain = ' <p>Using ConditionalGet and Encoder is straightforward. First impliment the ConditionalGet, then if the cache is not valid, encode and send the content</p> <p>This script emulates a document that changes every ' . $every . ' seconds. <br>This is version: ' . date('r', $lastModified) . '</p> '; require '_include.php'; $content = get_content(array('title' => $title, 'explain' => $explain)); $he = new HTTP_Encoder(array('content' => get_content(array('title' => $title, 'explain' => $explain)))); $he->encode(); // usually you would just $he->sendAll(), but here we want to emulate slow // connection $he->sendHeaders(); send_slowly($he->getContent());