PHP Web site caching speed up the way to share _php skills

Source: Internet
Author: User
Tags explode gz file strlen browser cache
Description
1, the server caches the compressed files, again access reduces the time to compress, reduce the CPU occupancy rate.
2, by setting the client file cache time, reduce the number of requests again, can be reduced by more than 85%.
3, because the picture is already compressed format, just set the client cache time, do not do compression processing.

How to use:
1, the server must support the Gzip,rewrite feature.
2, add the following code to the "Rewritebase/" line of the. htacess file, see figure
Rewriterule (. *.css$|. *.js$|. *.jpg$|. *.gif$|. *.png$) gzip.php?$1 [L]
3, upload gzip.php to root directory
4, in the root directory to build cache folder, guaranteed to read and write.


Copy Code code as follows:

<?php
/**
* @author Seraphim
* @copyright 2012
*/
<!--a common subroutine for returning headers-->
function Sendheader ($last _modified, $p _type, $content _length = 0)
{
Set Client cache validity time
Header ("Expires:"). Gmdate ("D, D M Y h:i:s", Time () + 15360000). "GMT");
Header ("cache-control:max-age=315360000");
Header ("Pragma:");
Set Last modification time
Header ("last-modified:"). $last _modified);
Setting File type information
Header ($p _type);
Header ("Content-length:"). $content _length);
}
Define (' Abspath ', DirName (__file__). '/');
$cache = true;
$cachedir = ' cache/'; A directory that stores the GZ files to ensure that they are writable
if (Empty ($_server[' query_string '))
Exit ();
$gzip = strstr ($_server[' http_accept_encoding '], ' gzip ');
if (empty ($gzip))
$cache = false;
$key = Array_shift (Explode ('? ', $_server[' query_string '));
$key = Str_replace ('.. /', ', $key);
$filename = Abspath. $key;
$symbol = ' _ ';
$rel _path = Str_replace (Abspath, ', DirName ($filename));
$namespace = Str_replace ('/', $symbol, $rel _path);
$cache _filename = Abspath. $cachedir. $namespace. $symbol. BaseName ($filename).
'. Gz '; Generate GZ file path
$ext = Array_pop (Explode ('. ', $filename)); Determine file type information based on suffix
$type = "content-type:text/html"; Default file type
Switch ($ext)
{
Case ' CSS ':
$type = "Content-type:text/css";
Break
Case ' JS ':
$type = "Content-type:text/javascript";
Break
Case ' gif ':
$cache = false;
$type = "Content-type:image/gif";
Break
Case ' jpg ':
$cache = false;
$type = "Content-type:image/jpeg";
Break
Case ' PNG ':
$cache = false;
$type = "Content-type:image/png";
Break
Default
Exit ();
}
if ($cache)
{
if (file_exists ($cache _filename))
{//If there is a GZ file
$mtime = Filemtime ($cache _filename);
$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
if ((Isset ($_server[' http_if_modified_since ')) && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
$GMT _mtime))
{
Consistent with the file modification date in the browser cache, returns 304
Header ("http/1.1 304 not Modified");
Send Client Header
Header ("Content-encoding:gzip");
Sendheader ($gmt _mtime, $type);
}
Else
{
Read GZ file output
$content = file_get_contents ($cache _filename);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
Send data
Echo $content;
}
}
Else
if (file_exists ($filename))
{//no corresponding GZ file
$mtime = Mktime ();
$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
Reading files
$content = file_get_contents ($filename);
Remove the blank part
$content = LTrim ($content);
Compress file Contents
$content = Gzencode ($content, 9, $gzip? Force_gzip:force_deflate);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
Send data
Echo $content;
Write to File
File_put_contents ($cache _filename, $content);
}
Else
{
Header ("http/1.0 404 Not Found");
}
}
Else
{//processing does not use output in gzip mode. Principle Basic Ditto
if (file_exists ($filename))
{
$mtime = Filemtime ($filename);
$gmt _mtime = gmdate (' d, D M Y h:i:s ', $mtime). ' GMT ';
if ((Isset ($_server[' http_if_modified_since ')) && array_shift (Explode ('; ', $_server[' http_if_modified_ SINCE ']) = =
$GMT _mtime))
{
Consistent with the file modification date in the browser cache, returns 304
Header ("http/1.1 304 not Modified");
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Header ("Content-encoding:gzip");
}
Else
{
Read file output
$content = file_get_contents ($filename);
Send Client Header
Sendheader ($gmt _mtime, $type, strlen ($content));
Send data
Echo $content;
}
}
Else
{
Header ("http/1.0 404 Not Found");
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.