mirror of
https://github.com/seigler/simple-php-buffering
synced 2025-07-26 17:26:12 +00:00
initial commit
This commit is contained in:
commit
a71ed489a2
4 changed files with 123 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# https://git-scm.com/docs/gitignore
|
||||||
|
# https://help.github.com/articles/ignoring-files
|
||||||
|
# Example .gitignore files: https://github.com/github/gitignore
|
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2016 Joshua Seigler
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
28
README.md
Normal file
28
README.md
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# seigler/simple-php-buffer
|
||||||
|
|
||||||
|
PHP file for basic file-based output buffering
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
```php
|
||||||
|
define('CACHE_PATH', sys_get_temp_dir().'/cache_'); // optional, cache-file location prefix
|
||||||
|
define('CACHE_TIME', 5 * 60); // optional, seconds to cache the output
|
||||||
|
require 'buffer.php';
|
||||||
|
```
|
||||||
|
|
||||||
|
This will send last-modified, expires, and etag headers. If a request includes theetag and
|
||||||
|
modified-since parameters the script can return a 304 not modified.
|
||||||
|
|
||||||
|
This uses output buffering, but it can't buffer headers. If you need to send out a header every time, cached or not, put that header code above the `require`.
|
||||||
|
|
||||||
|
```php
|
||||||
|
Header('Content-type: image/svg+xml; charset=utf-8');
|
||||||
|
Header('Content-Disposition: inline; filename="fancy-chart-' . date('Y-m-d\THisT') . '.svg"');
|
||||||
|
require 'buffer.php';
|
||||||
|
|
||||||
|
// remaining code to generate the chart
|
||||||
|
```
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* Based on http://www.the-art-of-web.com/php/buffer/
|
||||||
|
|
71
buffer.php
Normal file
71
buffer.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
// Based on code from The Art of Web: www.the-art-of-web.com
|
||||||
|
// Which was based on PHP code by Dennis Pallett: www.phpit.net
|
||||||
|
|
||||||
|
// location and prefix for cache files
|
||||||
|
defined('CACHE_PATH') or define('CACHE_PATH', sys_get_temp_dir().'/cache_');
|
||||||
|
|
||||||
|
// how long to keep the cache files (seconds)
|
||||||
|
defined('CACHE_TIME') or define('CACHE_TIME', 5 * 60);
|
||||||
|
|
||||||
|
// return location and name for cache file
|
||||||
|
function cache_file()
|
||||||
|
{
|
||||||
|
return CACHE_PATH . md5('buffer'.$_SERVER['REQUEST_URI']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// display cached file if present and not expired
|
||||||
|
function cache_display()
|
||||||
|
{
|
||||||
|
$file = cache_file();
|
||||||
|
|
||||||
|
// check that cache file exists and is not too old
|
||||||
|
if(!file_exists($file)) return;
|
||||||
|
$last_modified_time = filemtime($file);
|
||||||
|
if($last_modified_time < time() - CACHE_TIME) return;
|
||||||
|
$etag = md5_file($file);
|
||||||
|
|
||||||
|
// always send headers
|
||||||
|
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
|
||||||
|
header("Etag: $etag");
|
||||||
|
header("Expires: ".gmdate("D, d M Y H:i:s", $last_modified_time + CACHE_TIME)." GMT");
|
||||||
|
|
||||||
|
// tell any caches that I really mean it with these other headers
|
||||||
|
header("Cache-Control: max-age=".CACHE_TIME.", must-revalidate");
|
||||||
|
|
||||||
|
// HTTP 304 and exit if not modified
|
||||||
|
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
|
||||||
|
@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
|
||||||
|
header("HTTP/1.1 304 Not Modified");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if so, display cache file and stop processing
|
||||||
|
readfile($file);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// write to cache file
|
||||||
|
function cache_page($content)
|
||||||
|
{
|
||||||
|
if(false !== ($f = @fopen(cache_file(), 'w'))) {
|
||||||
|
fwrite($f, $content);
|
||||||
|
fclose($f);
|
||||||
|
}
|
||||||
|
$last_modified_time = time();
|
||||||
|
$etag = md5_file('buffer'.$file);
|
||||||
|
|
||||||
|
// always send headers
|
||||||
|
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
|
||||||
|
header("Etag: $etag");
|
||||||
|
header("Expires: ".gmdate("D, d M Y H:i:s", $last_modified_time + CACHE_TIME)." GMT");
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
// execution stops here if valid cache file found
|
||||||
|
cache_display();
|
||||||
|
|
||||||
|
// enable output buffering and create cache file
|
||||||
|
ob_start('cache_page');
|
||||||
|
?>
|
Loading…
Add table
Add a link
Reference in a new issue