Created
February 27, 2018 23:23
-
-
Save dejurin/b54329b0463183f43bcf41afa4b258e4 to your computer and use it in GitHub Desktop.
Code Igniter 3 Last modified and E-tag Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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