![]() | ||||||||||||||||||||||
|
|
![]() Not kehshirovat`! Modern browsers enough often use a local cache in the job. What is it means? It means that a browser, having received from the server the html-document, a picture or other resource, places it in the local cache (easier speaking, writes down the received resource on a hard disk of the machine of the user) and at the subsequent searches to such resource does not address on the server, and receives a resource from local kesha. Given the algorithm of job of browsers sharply raises speed of loading of html-documents. As if the resource was already loaded, and as consequence{investigation} is located in a local cache, time of access is defined{determined} not by bandwidth of a liaison channel (for example, modem connection) and speed of job of a hard disk. However alongside with advantages the given method as generates a number{line} of problems. In particular the majority of beginning{starting} web-programmers, by development of dynamic sites, faces the same problem. The essence of this problem consists that instead of the repeated reference{manipulation} on the server behind the page starting on the server a script, modifying a certain information, a browser addresses in a local cache. And as a result, for example three references{manipulations}, occurs not three updatings of the information located on the server, but only one. For what to force a browser each time to address for page on the server it is necessary to forbid for a browser to bring the given resource in a cache. The most widespread methods forbidding caching are below resulted or allowing it to bypass. Generation new URL We admit{allow} that the required resource has the following url: test.html? id=7. Apparently from url'b one parameter is passed him. We shall add, for example, with help JavaScript, in url one more parameter, and his value we shall make a random number. In result url will look as follows: test.html? id=7*rnd=0.6700820127538827. The casual parameter each time will be generated anew. The listing showing this approach below is resulted: <html> <head> <title> Generation new URL </title> <meta http-equiv = "Content-Type" content = " text/html; charset=windows-1251 "> </head> <body> <script language ='javascript '> document.write (" <a href = ' 1.html? id=7*rnd = " + Math.random () + " '> "); </script> The test link </a> </body> </html> Each time result of such search will be kehshirovat`sja but as caching is made on all url each time will turn out new url and the browser will be compelled to request a resource as url two searches will not coincide in accuracy from the server. Fields of headings To operate caching it is possible as on the part of the server. For this purpose the resource sent to a browser, is accompanied by fields of heading. The detailed description of fields of heading can be found in standard Rfc 2068 which describes report HTTP 1.1. Field of heading Expires Value of the given heading is date after which contents of a resource will become outdated. If the user after this date to address to a resource, the browser should request a resource of the server, instead of from a local cache. If the field> Expires <contains the date past{last}, in relation to current at the following reference{manipulation} to a resource the browser will be compelled to address again to the server. It will take place because or the document will not be brought in a cache - as already out-of-date, or at the reference{manipulation} to a cache the browser will define{determine}, that the document is already obsolete. The following listing on PHP shows use of heading Expires: header (" Expires: Mon, 26 Jul 1997 05:00:00 GMT "); Field of heading Last-Modified Value of the given heading is date of last updating of a resource. The majority of modern browsers use the following algorithm if the resource already is in a local cache: * Requests from the server date of last updating of a resource * Compares the received date and date of a resource in a local cache * If the resource on novee server of a resource in a cache - is requested a resource from the server If the resource located on the server, contains in the given field the current date a browser each time will request a resource from the server, instead of from a local cache. The following listing shows use of a field of heading Last-Modified: header (" Last-Modified: ". gmdate (" D, d M Y H:i:s "). "GMT"); Fields of heading Cache-Control and Pragma And, at last, the fields of heading directly responsible for caching of a resource. The field <Pragma> has been determined in standard Rfc 1945, describing report HTTP 1.0. The given field is considered out-of-date, but in some cases it is necessary to use it . In particular the some people of the proxy-server incorrectly process searches to constantly changing resources if together with a resource the given field of heading is not passed. The second field is determined in standard Rfc 2068 which describes report HTTP 1.1. The given field of heading allows to forbid caching, and each time to request a resource from the server. The following listing shows use of fields of heading Cache-Control and Pragma for an interdiction of caching: header (" Cache-Control: no-cache, must-revalidate "); header (" Pragma: no-cache "); |
|||||||||||||||||||||