Skip to content

Instantly share code, notes, and snippets.

@dejurin
Created February 27, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejurin/b54329b0463183f43bcf41afa4b258e4 to your computer and use it in GitHub Desktop.
Save dejurin/b54329b0463183f43bcf41afa4b258e4 to your computer and use it in GitHub Desktop.
Code Igniter 3 Last modified and E-tag Library
<?php
// Tested by Code Igniter 3.1.7
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Last_modified
{
protected $ci;
public function __construct()
{
$this->ci = get_instance();
}
public function set($etag, $timestamp)
{
$gmt_mtime = gmdate('r', $timestamp);
$this->ci->output->set_header('ETag: "'.md5($timestamp.$etag).'"');
$this->ci->output->set_header('Last-Modified: '.$gmt_mtime);
$this->ci->output->set_header('Cache-Control: public');
$et = trim($this->ci->input->server('HTTP_IF_NONE_MATCH'));
$f = strpos($et, '"');
$e = strrpos($et, '"');
if (null != $this->ci->input->server('HTTP_IF_MODIFIED_SINCE') || null != $this->ci->input->server('HTTP_IF_NONE_MATCH')) {
if ($this->ci->input->server('HTTP_IF_MODIFIED_SINCE') == $gmt_mtime || substr($et, $f + 1, -1) == md5($timestamp.$etag)) {
$this->ci->output->set_status_header(304);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment