PHP processing browser cache

Source: Internet
Author: User

It mainly involves three attributes in the header.

Lastmodified: Sets the last modification time. The browser sends a "If-modified-Sinc" header next time. If the content is not updated after this time, the server directly returns a 304 not modified message without transmitting details, which can save bandwidth.

Etag: Set a flag. The browser will send a "If-None-match" header next time. If the server content remains unchanged, it also returns a 304 not modified directly without transmitting the details, which can also save bandwidth.

Expires: Set an expiration time. If the current request is at this expiration timeDo not send HTTP requestsIt not only saves server bandwidth, but alsoReduces the number of HTTP requests on the server.

Cache-control: Max-age =Seconds cache relative timeSeconds is the cache time in seconds, that is, the browser's local cache does not access the server when the browser accesses the server within seconds after the first access.

 

The process is as follows:
1. The client requests a page ().
2. The server returns to page A and adds a last-modified/etag to page.
3. The client displays the page and caches the page together with last-modified/etag.
4. The customer requests page a again and passes the last-modified/etag returned by the server in the last request to the server.
5. The server checks the last-modified or etag and determines that the page has not been modified since the last client request. The server returns the response 304 and an empty response body.

 

Related functions

// HTTP header status code and description

$ Statustexts = array (
'20140901' => 'contine ',
'20140901' => 'switching Protocols ',
'20140901' => 'OK ',
'000000' => 'created ',
'20140901' => 'accepted ',
'000000' => 'non-authoritative information ',
'000000' => 'no content ',
'000000' => 'reset content ',
'000000' => 'partial content ',
'000000' => 'Multiple Choices ',
'20140901' => 'moved permanently ',
'20140901' => 'found ',
'000000' => 'see other ',
'20140901' => 'not modified ',
'20140901' => 'use proxy ',
'000000' => '(unused )',
'20140901' => 'temporary redirect ',
'20140901' => 'bad request ',
'20140901' => 'authorized ',
'000000' => 'payment required ',
'000000' => 'forbidden ',
'20140901' => 'not found ',
'20140901' => 'method not allowed ',
'20140901' => 'not accesstable ',
'20140901' => 'proxy authentication required ',
'20140901' => 'request timeout ',
'20140901' => 'canonicalization ',
'000000' => 'gione ',
'20140901' => 'length required ',
'20140901' => 'precondition failed ',
'000000' => 'request entity too large ',
'20140901' => 'request-URI Too long ',
'20140901' => 'unororted media type ',
'20140901' => 'requested range not satisfiable ',
'20140901' => 'pectation failed ',
'000000' => 'internal server error ',
'20140901' => 'not implemented ',
'20140901' => 'bad gateway ',
'20140901' => 'service unavailable ',
'20140901' => 'Gateway timeout ',
'20140901' => 'HTTP version not supported ted ',
);

// Set the cache expiration time

Function expires ($ seconds = 1800)
{
$ Time = Date ('d, D m y h: I: s', time () + $ seconds). 'gmt ';
Header ("expires: $ time ");

}

// Return the status information of the specified Header

Function statuscode ($ code, $ text = NULL)
{

Global $ statustexts; // HTTP header status
$ Protocol = isset ($ _ server ['server _ Protocol'])? $ _ Server ['server _ Protocol']: 'http/123 ';
$ Text = (null ===$ text )? $ Statustexts [$ Code]: $ text;
$ Status = "$ Protocol $ code $ text ";
Header ($ status );
}
// Lastmodified

Function lastmodified ($ modifiedtime, $ notmodifiedexit = true)
{
$ Modifiedtime = Date ('d, D m y h: I: S \ G \ m \ t', $ modifiedtime );
If ($ notmodifiedexit & isset ($ _ server ['HTTP _ if_modified_since ']) & $ modifiedtime ==$ _ server ['HTTP _ if_modified_since']) {
Statuscode ('20140901 ');
Exit ();
}
Header ("last-modified: $ modifiedtime ");

}

// Set the Charest page

Function charset ($ ENC = 'utf-8', $ type = 'text/html ')
{
Header ("Content-Type: $ type; charset = $ ENC ");
}

// Disable browser cache page

Function disablebrowsercache ()
{
Header ("expires: Mon, 26 Jul 1997 05:00:00 GMT ");
Header ("last-modified:". gmdate ("D, D m y h: I: s"). "GMT ");
Header ("cache-control: No-store, no-cache, must-revalidate ");
Header ("cache-control: Post-check = 0, pre-check = 0", false );
Header ("Pragma: No-Cache ");
}

// Etag sets the content tag etag to use MD5 for multiple purposes (the content to be output)

Function etag ($ etag, $ notmodifiedexit = true)
{
If ($ notmodifiedexit & isset ($ _ server ['HTTP _ if_none_match ']) & $ etag ==$ _ server ['HTTP _ if_none_match']) {
Statuscode ('20140901 ');
Exit ();
}
Header ("etag: $ etag ");

}

Http://hi.baidu.com/tenyaoshen/item/f7c99b4a86bba6e01281da0b

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.