Browsercache.php: A simple browser-cache-handling library for CodeIgniter
This content has been archived. Its subject matter is no longer relevant. This URL will remain active indefinitely, but the content will not otherwise be listed on the site.
Tags: Reading time: 1 minute Suggest a change
While CodeIgniter provides a disk caching mechanism, it lacks a browser caching mechanism. Without a lengthy introduction, here's one.
Browsercache.php
<?php if(! defined('BASEPATH')) exit();
class Browsercache
{
private $ci;
function __construct()
{
$this->ci =& get_instance();
}
public function cacheFor($minutes)
{
$this->ci->output->cache($minutes);
$this->ci->output->set_header(sprintf('Expires: %s GMT', gmdate('D, d M Y H:i:s', time() + ($minutes \* 60))));
}
public function dontCache()
{
$this->ci->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
$this->ci->output->set_header('Cache-Control: post-check=0, pre-check=0', false);
$this->ci->output->set_header('Pragma: no-cache');
}
}
Throw that in your application/libraries
folder, and use it like so:
- Cache a page for 30 minutes:
$this->browsercache->cacheFor(30);
- Prevent a page from being cached:
$this->browsercache->dontCache();
Yup. That's it. Use it wherever you want to.
I've also created a gist for it on GitHub.
Posted: