{"id":989,"date":"2019-08-21T16:30:08","date_gmt":"2019-08-21T08:30:08","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=989"},"modified":"2023-12-04T16:42:22","modified_gmt":"2023-12-04T08:42:22","slug":"codeigniter%ef%bc%889%ef%bc%89-%e8%be%93%e5%87%ba%e7%bb%84%e4%bb%b6output-php","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=989","title":{"rendered":"CodeIgniter\uff089\uff09\u2014\u2014\u8f93\u51fa\u7ec4\u4ef6Output.php"},"content":{"rendered":"<p>Output\u7ec4\u4ef6\u8d1f\u8d23\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u6700\u7ec8\u8f93\u51fa\uff0c\u662fci\u6846\u67b6\u4e2d\u4f7f\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u5728\u4e4b\u524d\u7684\u51e0\u7bc7\u6e90\u7801\u9605\u8bfb\u4e2d\uff0c\u4e5f\u4f7f\u7528\u5230\u4e86\u8fd9\u4e2a\u7ec4\u4ef6\uff0c\u4f8b\u5982\uff1aload->view() \u5728\u9605\u8bfb\u8f93\u51fa\u7ec4\u4ef6\u6e90\u7801\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e3b\u8981\u5173\u6ce8\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>Output\u662f\u5982\u4f55\u8f93\u51fa\u9875\u9762\u7684\uff0c\u5728\u6700\u7ec8\u8f93\u51fa\u7684\u4e4b\u524d\u505a\u4e86\u54ea\u4e9b\u5904\u7406\uff1f<\/li>\n<li>\u9875\u9762\u7f13\u5b58\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff1f<\/li>\n<\/ol>\n<h3>1.\u6784\u9020\u51fd\u6570<\/h3>\n<pre><code class=\"language-php line-numbers\">    public function __construct()\n    {\n        $this-&gt;_zlib_oc = (bool) ini_get('zlib.output_compression');\n        $this-&gt;_compress_output = (\n            $this-&gt;_zlib_oc === FALSE\n            &amp;&amp; config_item('compress_output') === TRUE\n            &amp;&amp; extension_loaded('zlib')\n        );\n\n        isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') &amp;&amp; ini_get('mbstring.func_overload'));\n\n        \/\/ Get mime types for later\n        $this-&gt;mimes =&amp; get_mimes();\n\n        log_message('info', 'Output Class Initialized');\n    }\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u4e00\u5171\u505a\u4e86\u4e09\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u538b\u7f29\u8f93\u51fa\u5185\u5bb9\uff0c\u8fd9\u662f\u4e3a\u4e86\u5728\u4e4b\u540e\u7684\u8f93\u51fa\u7f13\u5b58\u4e2d\u51b3\u5b9a\u662f\u5426\u538b\u7f29\uff1b<\/li>\n<li>mbstring\u662f\u5426\u53ef\u4ee5\u91cd\u8f7d\uff0c\u56e0\u4e3aci\u6846\u67b6\u4e2d\u5bf9mbstring\u6807\u51c6\u5e93\u7684\u4e00\u4e9b\u51fd\u6570\u8fdb\u884c\u4e86\u91cd\u8f7d\uff0c\u800c\u8f93\u51fa\u7ec4\u4ef6\u4e2d\u6b63\u597d\u8981\u4f7f\u7528\u91cd\u8f7d\u7684\u51fd\u6570\uff0c\u8be6\u7ec6\u4e86\u89e3\u53ef\u4ee5\u67e5\u770b&#8217;.\/system\/core\/compat\/mbstring.php&#8217;\u6587\u4ef6\uff1b<\/li>\n<li>ci\u6846\u67b6\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2amimes\u5217\u8868\uff0c\u5c06mimes\u5217\u8868\u53d6\u51fa\uff0c\u4e3a\u4e86\u4e4b\u540e\u8bbe\u7f6e\u8bf7\u6c42\u5934\u65f6\u4f7f\u7528\uff1b<\/li>\n<\/ol>\n<p>\u4ece\u6784\u9020\u51fd\u6570\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0coutput\u5728\u8f93\u51fa\u5185\u5bb9\u4e4b\u524d\uff0c\u4f1a\u5bf9\u8981\u8f93\u51fa\u7684\u5185\u5bb9\u8fdb\u884c\u538b\u7f29\u3002<\/p>\n<h3>2.\u8f93\u51fa\u51fd\u6570\u2014\u2014_display()<\/h3>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Display Output\n     *\n     * Processes and sends finalized output data to the browser along\n     * with any server headers and profile data. It also stops benchmark\n     * timers so the page rendering speed and memory usage can be shown.\n     *\n     * Note: All \"view\" data is automatically put into $this-&gt;final_output\n     *   by controller class.\n     *\n     * @uses    CI_Output::$final_output\n     * @param   string  $output Output data override\n     * @return  void\n     *\/\n    public function _display($output = '')\n    {\n        \/\/ Note:  We use load_class() because we can't use $CI =&amp; get_instance()\n        \/\/ since this function is sometimes called by the caching mechanism,\n        \/\/ which happens before the CI super object is available.\n        $BM =&amp; load_class('Benchmark', 'core');\n        $CFG =&amp; load_class('Config', 'core');\n\n        \/\/ Grab the super object if we can.\n        if (class_exists('CI_Controller', FALSE))\n        {\n            $CI =&amp; get_instance();\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Set the output data\n        if ($output === '')\n        {\n            $output =&amp; $this-&gt;final_output;\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Do we need to write a cache file? Only if the controller does not have its\n        \/\/ own _output() method and we are not dealing with a cache file, which we\n        \/\/ can determine by the existence of the $CI object above\n        if ($this-&gt;cache_expiration &gt; 0 &amp;&amp; isset($CI) &amp;&amp; ! method_exists($CI, '_output'))\n        {\n            $this-&gt;_write_cache($output);\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Parse out the elapsed time and memory usage,\n        \/\/ then swap the pseudo-variables with the data\n\n        $elapsed = $BM-&gt;elapsed_time('total_execution_time_start', 'total_execution_time_end');\n\n        if ($this-&gt;parse_exec_vars === TRUE)\n        {\n            $memory = round(memory_get_usage() \/ 1024 \/ 1024, 2).'MB';\n            $output = str_replace(array('{elapsed_time}', '{memory_usage}'), array($elapsed, $memory), $output);\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Is compression requested?\n        if (isset($CI) \/\/ This means that we're not serving a cache file, if we were, it would already be compressed\n            &amp;&amp; $this-&gt;_compress_output === TRUE\n            &amp;&amp; isset($_SERVER['HTTP_ACCEPT_ENCODING']) &amp;&amp; strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)\n        {\n            ob_start('ob_gzhandler');\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Are there any server headers to send?\n        if (count($this-&gt;headers) &gt; 0)\n        {\n            foreach ($this-&gt;headers as $header)\n            {\n                @header($header[0], $header[1]);\n            }\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Does the $CI object exist?\n        \/\/ If not we know we are dealing with a cache file so we'll\n        \/\/ simply echo out the data and exit.\n        if ( ! isset($CI))\n        {\n            if ($this-&gt;_compress_output === TRUE)\n            {\n                if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) &amp;&amp; strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)\n                {\n                    header('Content-Encoding: gzip');\n                    header('Content-Length: '.self::strlen($output));\n                }\n                else\n                {\n                    \/\/ User agent doesn't support gzip compression,\n                    \/\/ so we'll have to decompress our cache\n                    $output = gzinflate(self::substr($output, 10, -8));\n                }\n            }\n\n            echo $output;\n            log_message('info', 'Final output sent to browser');\n            log_message('debug', 'Total execution time: '.$elapsed);\n            return;\n        }\n\n        \/\/ --------------------------------------------------------------------\n\n        \/\/ Do we need to generate profile data?\n        \/\/ If so, load the Profile class and run it.\n        if ($this-&gt;enable_profiler === TRUE)\n        {\n            $CI-&gt;load-&gt;library('profiler');\n            if ( ! empty($this-&gt;_profiler_sections))\n            {\n                $CI-&gt;profiler-&gt;set_sections($this-&gt;_profiler_sections);\n            }\n\n            \/\/ If the output data contains closing &lt;\/body&gt; and &lt;\/html&gt; tags\n            \/\/ we will remove them and add them back after we insert the profile data\n            $output = preg_replace('|&lt;\/body&gt;.*?&lt;\/html&gt;|is', '', $output, -1, $count).$CI-&gt;profiler-&gt;run();\n            if ($count &gt; 0)\n            {\n                $output .= '&lt;\/body&gt;&lt;\/html&gt;';\n            }\n        }\n\n        \/\/ Does the controller contain a function named _output()?\n        \/\/ If so send the output there.  Otherwise, echo it.\n        if (method_exists($CI, '_output'))\n        {\n            $CI-&gt;_output($output);\n        }\n        else\n        {\n            echo $output; \/\/ Send it to the browser!\n        }\n\n        log_message('info', 'Final output sent to browser');\n        log_message('debug', 'Total execution time: '.$elapsed);\n    }\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u4e3b\u8981\u6ce8\u610f\u7684\u662f\u538b\u7f29\u95ee\u9898\u3002<\/p>\n<ol>\n<li>$_SERVER[&#8216;HTTP_ACCEPT_ENCODING&#8217;]\u662f\u5224\u65ad\u5ba2\u6237\u7aef\u662f\u5426\u652f\u6301\u538b\u7f29<\/li>\n<li>\u5982\u679c\u652f\u6301\u538b\u7f29\u4e14\u9700\u8981\u6267\u884cphp\u538b\u7f29\u4ee3\u7801\uff0c\u5982\u679c\u662f\u7f13\u5b58\uff0c\u53d1\u9001\u76f8\u5e94\u8bf7\u6c42\u5934\uff0c\u4e0d\u662f\u7f13\u5b58\uff0c\u6267\u884cphp\u7684\u538b\u7f29\u4ee3\u7801<\/li>\n<li>\u5982\u679c\u4e0d\u652f\u6301\u538b\u7f29\uff0c\u5982\u679c\u662f\u7f13\u5b58\uff0c\u5219\u5728\u6267\u884cphp\u7684\u89e3\u538b\u7f29\u4ee3\u7801\u518d\u8f93\u51fa\uff08\u56e0\u4e3a\u7f13\u5b58\u7684\u5185\u5bb9\u4f1a\u8fdb\u884c\u538b\u7f29\uff09\uff0c\u4e0d\u662f\u7f13\u5b58\uff0c\u4e0d\u6267\u884c\u538b\u7f29\u4ee3\u7801<\/li>\n<\/ol>\n<h3>3.\u5199\u7f13\u5b58_write_cache<\/h3>\n<p>\u7f13\u5b58\u7684\u76ee\u5f55\u53ef\u4ee5\u662f\u81ea\u5b9a\u4e49\u76ee\u5f55\uff0c\u9ed8\u8ba4\u662f&#8217;application\/cache&#8217;\u76ee\u5f55<\/p>\n<pre><code class=\"language-php line-numbers\">$path = $CI-&gt;config-&gt;item('cache_path');\n        $cache_path = ($path === '') ? APPPATH.'cache\/' : $path;\n<\/code><\/pre>\n<p>\u7f13\u5b58\u7684key\u662f\u6839\u636euri\u4fe1\u606f\u751f\u6210\u7684md5\u5b57\u7b26\u4e32<\/p>\n<pre><code class=\"language-php line-numbers\">$uri = $CI-&gt;config-&gt;item('base_url')\n            .$CI-&gt;config-&gt;item('index_page')\n            .$CI-&gt;uri-&gt;uri_string();\n\n        if (($cache_query_string = $CI-&gt;config-&gt;item('cache_query_string')) &amp;&amp; ! empty($_SERVER['QUERY_STRING']))\n        {\n            if (is_array($cache_query_string))\n            {\n                $uri .= '?'.http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));\n            }\n            else\n            {\n                $uri .= '?'.$_SERVER['QUERY_STRING'];\n            }\n        }\n\n        $cache_path .= md5($uri);\n<\/code><\/pre>\n<p>$this->_compress_output\u4e3atrue\u5bf9\u8f93\u51fa\u5185\u5bb9\u6267\u884cphp\u538b\u7f29\u4ee3\u7801<\/p>\n<pre><code class=\"language-php line-numbers\">if ($this-&gt;_compress_output === TRUE)\n        {\n            $output = gzencode($output);\n\n            if ($this-&gt;get_header('content-type') === NULL)\n            {\n                $this-&gt;set_content_type($this-&gt;mime_type);\n            }\n        }\n<\/code><\/pre>\n<p>\u7f13\u5b58\u6709\u6548\u65f6\u95f4\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff0c\u7f13\u5b58\u6709\u6548\u65f6\u95f4\u548c\u8bf7\u6c42\u5934\u4fe1\u606f\uff0c\u4ee5\u5e8f\u5217\u5316\u540e\u7684\u6570\u7ec4\u5b58\u5165\u7f13\u5b58\u4e2d\uff0c\u5e76\u4ee5&#8217;ENDCI&#8212;>&#8217;\u5b57\u7b26\u4e32\u4e3a\u5206\u5272\u7b26\u5408\u8f93\u51fa\u5185\u5bb9\u5206\u5f00<\/p>\n<pre><code class=\"language-php line-numbers\"><br \/>        $expire = time() + ($this-&gt;cache_expiration * 60);\n\n        \/\/ Put together our serialized info.\n        $cache_info = serialize(array(\n            'expire'    =&gt; $expire,\n            'headers'   =&gt; $this-&gt;headers\n        ));\n\n        $output = $cache_info.'ENDCI---&gt;'.$output;\n<\/code><\/pre>\n<p>\u6700\u540e\u5c06\u7f13\u5b58\u5185\u5bb9\u5199\u5165\u7f13\u5b58\u6587\u4ef6<\/p>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Write Cache\n     *\n     * @param   string  $output Output data to cache\n     * @return  void\n     *\/\n    public function _write_cache($output)\n    {\n        $CI =&amp; get_instance();\n        $path = $CI-&gt;config-&gt;item('cache_path');\n        $cache_path = ($path === '') ? APPPATH.'cache\/' : $path;\n\n        if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))\n        {\n            log_message('error', 'Unable to write cache file: '.$cache_path);\n            return;\n        }\n\n        $uri = $CI-&gt;config-&gt;item('base_url')\n            .$CI-&gt;config-&gt;item('index_page')\n            .$CI-&gt;uri-&gt;uri_string();\n\n        if (($cache_query_string = $CI-&gt;config-&gt;item('cache_query_string')) &amp;&amp; ! empty($_SERVER['QUERY_STRING']))\n        {\n            if (is_array($cache_query_string))\n            {\n                $uri .= '?'.http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));\n            }\n            else\n            {\n                $uri .= '?'.$_SERVER['QUERY_STRING'];\n            }\n        }\n\n        $cache_path .= md5($uri);\n\n        if ( ! $fp = @fopen($cache_path, 'w+b'))\n        {\n            log_message('error', 'Unable to write cache file: '.$cache_path);\n            return;\n        }\n\n        if ( ! flock($fp, LOCK_EX))\n        {\n            log_message('error', 'Unable to secure a file lock for file at: '.$cache_path);\n            fclose($fp);\n            return;\n        }\n\n        \/\/ If output compression is enabled, compress the cache\n        \/\/ itself, so that we don't have to do that each time\n        \/\/ we're serving it\n        if ($this-&gt;_compress_output === TRUE)\n        {\n            $output = gzencode($output);\n\n            if ($this-&gt;get_header('content-type') === NULL)\n            {\n                $this-&gt;set_content_type($this-&gt;mime_type);\n            }\n        }\n\n        $expire = time() + ($this-&gt;cache_expiration * 60);\n\n        \/\/ Put together our serialized info.\n        $cache_info = serialize(array(\n            'expire'    =&gt; $expire,\n            'headers'   =&gt; $this-&gt;headers\n        ));\n\n        $output = $cache_info.'ENDCI---&gt;'.$output;\n\n        for ($written = 0, $length = self::strlen($output); $written &lt; $length; $written += $result)\n        {\n            if (($result = fwrite($fp, self::substr($output, $written))) === FALSE)\n            {\n                break;\n            }\n        }\n\n        flock($fp, LOCK_UN);\n        fclose($fp);\n\n        if ( ! is_int($result))\n        {\n            @unlink($cache_path);\n            log_message('error', 'Unable to write the complete cache content at: '.$cache_path);\n            return;\n        }\n\n        chmod($cache_path, 0640);\n        log_message('debug', 'Cache file written: '.$cache_path);\n\n        \/\/ Send HTTP cache-control headers to browser to match file cache settings.\n        $this-&gt;set_cache_header($_SERVER['REQUEST_TIME'], $expire);\n    }\n<\/code><\/pre>\n<h3>4.\u8f93\u51fa\u7f13\u5b58\u5185\u5bb9\u2014\u2014_display_cache\u51fd\u6570<\/h3>\n<p>_display_cache\u4f1a\u901a\u8fc7Key\u627e\u5230\u5bf9\u5e94\u7684\u7f13\u5b58\u6587\u4ef6\uff0c\u7136\u540e\u53d6\u51fa\u7f13\u5b58\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\uff0c\u67e5\u770b\u7f13\u5b58\u4fe1\u606f\uff0c\u53cd\u5e8f\u5217\u5316\u7f13\u5b58\u4fe1\u606f\uff0c\u5f97\u5230\u7f13\u5b58\u6709\u6548\u65f6\u95f4\uff0c\u5224\u65ad\u7f13\u5b58\u662f\u5426\u6709\u6548\uff0c\u65e0\u6548\u5219\u5220\u9664\u7f13\u5b58\uff0c\u8fd4\u56deFALSE *$_SERVER[&#8216;REQUEST_TIME&#8217;]\u53c2\u6570\u662f\u8bf7\u6c42\u65f6\u95f4<\/p>\n<pre><code class=\"language-php line-numbers\">        if ( ! preg_match('\/^(.*)ENDCI---&gt;\/', $cache, $match))\n        {\n            return FALSE;\n        }\n\n        $cache_info = unserialize($match[1]);\n        $expire = $cache_info['expire'];\n\n        $last_modified = filemtime($filepath);\n\n        \/\/ Has the file expired?\n        if ($_SERVER['REQUEST_TIME'] &gt;= $expire &amp;&amp; is_really_writable($cache_path))\n        {\n            \/\/ If so we'll delete it.\n            @unlink($filepath);\n            log_message('debug', 'Cache file has expired. File deleted.');\n            return FALSE;\n        }\n<\/code><\/pre>\n<p>\u8bbe\u7f6e\u7f13\u5b58\u8bf7\u6c42\u5934<\/p>\n<pre><code class=\"language-php line-numbers\"><br \/>        \/\/ Send the HTTP cache control headers\n        $this-&gt;set_cache_header($last_modified, $expire);\n\n        \/\/ Add headers from cache file.\n        foreach ($cache_info['headers'] as $header)\n        {\n            $this-&gt;set_header($header[0], $header[1]);\n        }\n\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7f13\u5b58 \u5728\u9605\u8bfbCodeIngiter.php\u7684\u65f6\u5019\uff0c\u6211\u4eec\u77e5\u9053\uff0c\u5982\u679c\u5bf9\u5e94\u8bf7\u6c42\u6709\u7f13\u5b58\u7684\u8bdd\uff0c\u5219\u4e0d\u4f1a\u521d\u59cb\u5316\u63a7\u5236\u5668\uff0c\u6240\u4ee5_display\u5c31\u662f\u901a\u8fc7\u662f\u5426\u52a0\u8f7d\u4e86\u63a7\u5236\u5668\u6765\u5224\u65ad\u8f93\u51fa\u7684\u5185\u5bb9\u662f\u4e0d\u662f\u7f13\u5b58\u7684<\/p>\n<pre><code class=\"language-php line-numbers\">        \/\/ Display the cache\n        $this-&gt;_display(self::substr($cache, self::strlen($match[0])));\n<\/code><\/pre>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Update\/serve cached output\n     *\n     * @uses    CI_Config\n     * @uses    CI_URI\n     *\n     * @param   object  &amp;$CFG   CI_Config class instance\n     * @param   object  &amp;$URI   CI_URI class instance\n     * @return  bool    TRUE on success or FALSE on failure\n     *\/\n    public function _display_cache(&amp;$CFG, &amp;$URI)\n    {\n        $cache_path = ($CFG-&gt;item('cache_path') === '') ? APPPATH.'cache\/' : $CFG-&gt;item('cache_path');\n\n        \/\/ Build the file path. The file name is an MD5 hash of the full URI\n        $uri = $CFG-&gt;item('base_url').$CFG-&gt;item('index_page').$URI-&gt;uri_string;\n\n        if (($cache_query_string = $CFG-&gt;item('cache_query_string')) &amp;&amp; ! empty($_SERVER['QUERY_STRING']))\n        {\n            if (is_array($cache_query_string))\n            {\n                $uri .= '?'.http_build_query(array_intersect_key($_GET, array_flip($cache_query_string)));\n            }\n            else\n            {\n                $uri .= '?'.$_SERVER['QUERY_STRING'];\n            }\n        }\n\n        $filepath = $cache_path.md5($uri);\n\n        if ( ! file_exists($filepath) OR ! $fp = @fopen($filepath, 'rb'))\n        {\n            return FALSE;\n        }\n\n        flock($fp, LOCK_SH);\n\n        $cache = (filesize($filepath) &gt; 0) ? fread($fp, filesize($filepath)) : '';\n\n        flock($fp, LOCK_UN);\n        fclose($fp);\n\n        \/\/ Look for embedded serialized file info.\n        if ( ! preg_match('\/^(.*)ENDCI---&gt;\/', $cache, $match))\n        {\n            return FALSE;\n        }\n\n        $cache_info = unserialize($match[1]);\n        $expire = $cache_info['expire'];\n\n        $last_modified = filemtime($filepath);\n\n        \/\/ Has the file expired?\n        if ($_SERVER['REQUEST_TIME'] &gt;= $expire &amp;&amp; is_really_writable($cache_path))\n        {\n            \/\/ If so we'll delete it.\n            @unlink($filepath);\n            log_message('debug', 'Cache file has expired. File deleted.');\n            return FALSE;\n        }\n\n        \/\/ Send the HTTP cache control headers\n        $this-&gt;set_cache_header($last_modified, $expire);\n\n        \/\/ Add headers from cache file.\n        foreach ($cache_info['headers'] as $header)\n        {\n            $this-&gt;set_header($header[0], $header[1]);\n        }\n\n        \/\/ Display the cache\n        $this-&gt;_display(self::substr($cache, self::strlen($match[0])));\n        log_message('debug', 'Cache file is current. Sending it to browser.');\n        return TRUE;\n    }\n\n<\/code><\/pre>\n<h3>5.\u8bbe\u7f6e\u7f13\u5b58\u8bf7\u6c42\u5934\u2014\u2014set_cache_header\u51fd\u6570<\/h3>\n<pre><code class=\"language-php line-numbers\">    \/**\n     * Set Cache Header\n     *\n     * Set the HTTP headers to match the server-side file cache settings\n     * in order to reduce bandwidth.\n     *\n     * @param   int $last_modified  Timestamp of when the page was last modified\n     * @param   int $expiration Timestamp of when should the requested page expire from cache\n     * @return  void\n     *\/\n    public function set_cache_header($last_modified, $expiration)\n    {\n        $max_age = $expiration - $_SERVER['REQUEST_TIME'];\n\n        if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &amp;&amp; $last_modified &lt;= strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))\n        {\n            $this-&gt;set_status_header(304);\n            exit;\n        }\n\n        header('Pragma: public');\n        header('Cache-Control: max-age='.$max_age.', public');\n        header('Expires: '.gmdate('D, d M Y H:i:s', $expiration).' GMT');\n        header('Last-modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT');\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Output\u7ec4\u4ef6\u8d1f\u8d23\u5411\u6d4f\u89c8\u5668\u53d1\u9001\u6700\u7ec8\u8f93\u51fa\uff0c\u662fci\u6846\u67b6\u4e2d\u4f7f\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u5728\u4e4b\u524d\u7684\u51e0\u7bc7\u6e90\u7801\u9605\u8bfb\u4e2d\uff0c\u4e5f\u4f7f\u7528\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[405],"class_list":["post-989","post","type-post","status-publish","format-standard","hentry","category-15","tag-php"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=989"}],"version-history":[{"count":1,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/989\/revisions"}],"predecessor-version":[{"id":990,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/989\/revisions\/990"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}