The PHP version of the website cache to speed up the opening of the way to share, the need for friends can refer to the following instructions:
1. the compressed files are cached on the server, and the re-access reduces the re-compression time to reduce the CPU usage.
2. you can set the client file cache time to reduce the number of re-requests by more than 85%.
3. because the image is already in the compression format, it only sets the client cache time and does not compress the image.
Usage:
1. the server must support the gzip and Rewrite functions.
2. add the following code under the RewriteBase/line of the. htacess file, as shown in the figure below.
RewriteRule (. *. css $ |. *. js $ |. *. jpg $ |. *. gif $ |. *. png $) gzip. php? $1 [L]
3. Upload gzip. php to the root directory.
4. create a cache folder in the root directory to ensure read/write.
The code is as follows:
/**
* @ Author Seraphim
* @ Copyright 2012
*/
//
Function sendheader ($ last_modified, $ p_type, $ content_length = 0)
{
// Set the client cache validity period
Header ("Expires:". gmdate ("D, d m y h: I: s", time () + 15360000). "GMT ");
Header ("Cache-Control: max-age = 315360000 ");
Header ("Pragma :");
// Set the last modification time
Header ("Last-Modified:". $ last_modified );
// Set file type information
Header ($ p_type );
Header ("Content-Length:". $ content_length );
}
Define ('abspath', dirname (_ file __).'/');
$ Cache = true;
$ Cachedir = 'cache/'; // Directory for storing the gz file to ensure that the file can be written
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 the gz file path
$ Ext = array_pop (explode ('.', $ filename); // you can specify the file type based on the 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 the gz file exists
$ 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 ))
{
// The file modification date is the same as that in the browser cache, and 304 is returned.
Header ("HTTP/1.1 304 Not Modified ");
// Send the client header
Header ("Content-Encoding: gzip ");
Sendheader ($ gmt_mtime, $ type );
}
Else
{
// Read the gz file output
$ Content = file_get_contents ($ cache_filename );
// Send the 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 ';
// Read the file
$ Content = file_get_contents ($ filename );
// Remove the blank part
// $ Content = ltrim ($ content );
// Compress the file content
$ Content = gzencode ($ content, 9, $ gzip? FORCE_GZIP: FORCE_DEFLATE );
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
Header ("Content-Encoding: gzip ");
// Send data
Echo $ content;
// Write a file
File_put_contents ($ cache_filename, $ content );
}
Else
{
Header ("HTTP/1.0 404 Not Found ");
}
}
Else
{// The output in Gzip mode is not used for processing. The principle is basically the same as above.
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 ))
{
// The file modification date is the same as that in the browser cache, and 304 is returned.
Header ("HTTP/1.1 304 Not Modified ");
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
Header ("Content-Encoding: gzip ");
}
Else
{
// Read file output
$ Content = file_get_contents ($ filename );
// Send the client header
Sendheader ($ gmt_mtime, $ type, strlen ($ content ));
// Send data
Echo $ content;
}
}
Else
{
Header ("HTTP/1.0 404 Not Found ");
}
}
?>
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.