1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php function getUrl () { if (!isset($_SERVER['REQUEST_URI'])) { $url = $_SERVER['REQUEST_URI']; } else { $url = $_SERVER['SCRIPT_NAME']; $url .= (!empty($_SERVER['QUERY_STRING']))? '?' . $_SERVER[ 'QUERY_STRING' ] : ''; } return $url; } //getUrl gets the queried page with query string function cache ($buffer) { //page's content is $buffer $url = getUrl(); if(is_dir(__DIR__ ."/cache")); else mkdir(__DIR__ ."/cache"); $filename = md5($url) . '.cache'; $data = time() . '¦' . $buffer; $filew = fopen(__DIR__ ."/cache/" . $filename, 'w'); fwrite($filew, $data); fclose($filew); return $buffer; } function display () { $url = getUrl(); $filename = md5($url) . '.cache'; if (!file_exists(__DIR__ ."/cache/" . $filename)) { return false; } $filer = fopen(__DIR__ ."/cache/" . $filename, 'r'); $data = fread($filer, filesize(__DIR__ ."/cache/" . $filename)); fclose($filer); $content = explode('¦', $data, 2); if (count($content)!= 2 OR !is_numeric($content['0'])) { return false; } if (time()-(100) > $content['0']) { // 100 is the cache time here!!! return false; } echo $content['1']; die(); } // Display cache (if any) display(); // if it is displayed, die function will end the program here. // if no cache, callback cache ob_start ('cache'); ?> |
Leave a reply