Php disable caching. Prohibiting page caching using Apache.htaccess

The article will be useful to visitors and site owners who care that the information received from their resources is timely and reliable. After reading this material, you will understand why visitors may see outdated information on the site, and you will also learn ways to solve this problem.


Currently, the speed of data transfer on the Internet makes it easy to watch videos in good quality, play online games, organize conferences, quickly download and transfer information, etc. But it was not always so! Ten years ago, in order to transfer 1 MB of information, you had to spend a lot of time. To increase the loading speed of site pages, it was decided to save some of the data in the browser cache (memory) on local computer user. During subsequent access to the site, the browser downloaded them from there, provided that the downloaded data did not change.

Nowadays, the content of data in the browser cache is not so popular in order to increase loading speed. In most cases, data is requested from the server GET methods or HEAD. Requests using the GET and HEAD methods are cached (saved) in the server and proxy logs, as well as in the browser history by default. Due to the fact that information is cached, many site visitors will see outdated data, and new information will become available to them only after it is deleted from the cache. This can cause problems not only for site visitors, but also affect the company's profits if the site is commercial.

To solve this problem, you need to disable caching of data transmitted from the server. There are many ways to disable caching described on the Internet: they all differ from each other, and some no longer work.

The most common rules are published on the authoritative resources www.spravkaweb.ru (http://www.spravkaweb.ru/php/sovet/nocash) and www.ru.wikipedia.org (http://ru.wikipedia.org/wiki/ Meta tags). Many sites have borrowed these settings and use them now.

According to Wikipedia, if you write in the title of a document

Another authoritative resource www.spravkaweb.ru claims that four headers can be passed that will prohibit data caching under any conditions.

With these settings, the server sends the headers shown in Fig. 3.

Fig.3 Server response headers

Let's look at each line to understand how it should work.

The title of the relevance of the document is proposed to be transmitted as a past date. For example, “Expires: Mon, 26 Jul 1997 05:00:00 GMT”, thereby determining that the data is no longer needed and there is no reason to cache it. This should arouse suspicion not only among people, but also among search robots when indexing such documents. Why should a robot be kept in the index? irrelevant information, few people think about it. Search engines They don’t like it when people try to give them false information.

The "Last-Modified" header tells the browser the date and time the document was modified (in in this example date and time will be current). If every time you visit the page search robot reports that the document has just changed (although in its search index the robot does not notice the differences), then perhaps the system will perceive this as an impact on relevance search results in order to improve the site's position. This may cause sanctions to be imposed by search engines.

Passing the "Pragma: no-cache" header to disable data caching was one of the first methods, but this moment no longer works.

Experiment

Example 1.

Let's conduct an experiment. Let's create a cache.php document in which we will give the headers from the example to check if it works this method prohibiting data caching. We will request from the server current time, and see where the information comes from - from the browser cache or downloaded from the server.

During the experiment, we will follow the link to the cache .php file located on the real server and observe whether the time is updated or not. If the time is not updated, then the data is taken from the browser cache and the ban does not work.

The time indicator is not updated, and this once again proves that most resources contain false information to prohibit caching. Such mass copying of other people's mistakes is of great interest not only in specific case, but also throughout the Internet in general.

In order to show a working method of disabling caching, I will give a second example, in which its performance will also be tested through an experiment.

Example 2.

The first example suggested sending the “Cache-Control: no-cache, must-revalidate” header, but it was configured incorrectly. Let's add new parameters to the header: “Cache-Control: no-cache, no-store, must-revalidate, max-age=0”, where

no-store - private data cannot be stored on local disk(disables caching in browsers);

no-cache - directive used to prohibit caching of information by proxy servers;

max-age=0 - directive indicating the time during which information is stored in the browser cache;

must-revalidate - a directive that tells the browser about the requirement additional check the relevance of the document in case of doubt.

Let's create a cache2.php document in which we will send new headers to check whether this method of disabling data caching works.

The procedure and conditions of the experiment remain the same. If the time changes, it means it is being requested from the server and not from the browser cache.

Since the time is updated every time the page is accessed, the disable caching method actually works. In this example, the parameters are passed from the page itself, but a more universal option is possible, which is configured on the server. On server Apache settings are made through the .htaccess service file, this provides a more functional mechanism for disabling caching for all pages of the site at once. In the configuration file, you must enable the modules mod_headers.c and mod_expires.c, removing comments from the code fragment

and add a piece of code to the .htaccess file

If the server is Internet Information Services (IIS) 6.0, then caching can only be disabled by having access directly to the server interface. This can be done in the “HTTP Headers” tab in the server settings.

In Fig. Figure 6 shows an example of disabling caching by transmitting the “Cache-Control: no-cache, no-store, must-revalidate, max-age=0” headers for site pages. To configure the "Expires" header, you need to enable "Enable content expiration" and "Expires immediate".

Disadvantages of disabling caching

Disabling caching increases the load on the server on which the site is located, because it has to respond to large quantity requests. For site visitors, disabling caching reduces page loading speed, but this is only noticeable when slow speed Internet.

Conclusion

Timely receipt of information plays a significant role both for site visitors and their owners. Therefore, owners of web resources need to carefully analyze the headers that are sent by their servers. Disabling caching is important setting, which allows visitors to always receive only real information from your site. Nowadays, most Internet resources display the headings “Expires” and “Last-Modified”, which not only do not prohibit caching, but may also be able to prevent normal indexing of the site by search robots.

Webmasters often encounter caching: browsers and proxy servers, trying to speed up the Web for the user, try to keep as much a large number of documents in the cache.

Sometimes it is necessary to prevent the browser from caching a page, since the information on it is updated every time. This could be the generation of data according to selected filters or other content that is created in a new way each time. In short, there are times when it is necessary to prevent an insidious program from caching a page. Today, we will learn how to implement this different ways, With using PHP or HTML or .htaccess

Prohibiting page caching in HTML

This can be done using meta tags.
Prohibition of caching by browser and proxy server

Disable page caching, browser only

Setting caching to certain time, for browser

Using the code below, we can tell the browser how long to keep the document in cache. After which, the cache will be updated.

Setting caching for a specific time for a proxy server
Practically, the same as in the previous code, only the indication is specific to the proxy server.

Prevent page caching using PHP

Practically, everything is the same as in the case of HTML, only we will display information through header headers. Here's how to implement an absolute cache denial:

You can also allow caching for a certain time. For example, let's allow caching for only 1 hour.

Prevent page caching using .htaccess

To make the idea easier to implement, everything can be done at the configuration level Apache server. Before this, we need to make sure that the necessary modules are in working order. Opening configuration file Apache and see the following picture:

LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
...
AddModule mod_expires.c
AddModule mod_headers.c

Now in the .htaccess file, we actually prohibit caching of the output data. As we know, the .htaccess file will be distributed to the directory in which it is located and to all subdirectories.

# Header Cache-Control Header append Cache-Control "no-store, no-cache, must-revalidate" # Header Expires ExpiresActive On ExpiresDefault "now"

It is important to note that completely prohibiting caching increases the load on the server. Therefore, play with this carefully! Better yet, set a specific time for which you can cache documents. For example, let's set caching for 1 hour:

# Header Cache-Control Header append Cache-Control "public" # Header Expires ExpiresActive On ExpiresDefault "access plus 1 hours"

Disable caching using PHP

Disable caching using PHP

Most scripts generate documents that change each time the program is launched. Obviously, if the user's browser starts caching such documents, nothing good will happen.

You can prevent the browser and Proxy servers from caching documents using the following tools: PHP language, namely the Header() functions.

To do this, use the following commands at the beginning of the script:

Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //Date in the past Header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 Header("Pragma: no-cache"); // HTTP/1.1 Header("Last-Modified: ".gmdate("D, d M Y H:i:s")."GMT");

To completely disable caching, you must always send 4 specified headers, and not a single one can be skipped - otherwise either the browser or the Proxy server will not work.

From the book Boost your website author Nikolay Matsievsky

Caching Specification RFC-2616 has an entire chapter dedicated to HTTP caching. It goes into detail about what the individual headings mean. Let's look at the key points. The Expires header sets the time at which the information is relevant. For resources,

From book Windows Registry author Klimov Alexander

Practical disabling caching You can also disable caching directly from the Apache configuration (detailed configuration for optimal performance is given in Chapter Eight). For this we need following lines:# Check that mod_headers is connected# Then set

From the book Working on the Internet by Dmitry Makarsky

Allowing caching When disabling caching, we will force the browser to re-download documents and resource files each time. In the latter case, this is not at all optimal and can lead to a noticeable slowdown in working with the site. Let's look at how you can set a deadline

From book Effective use C++. 55 the right ways improve the structure and code of your programs by Meyers Scott

Disable password caching ( Internet Explorer 4 and above) This setting is reportedly of limited use. The information provided applies to the Internet. Explorer versions 4.01 with the 2nd update package installed, to 5 and 5.01 Internet versions Explorer running under Windows 95, 98, NT 4.0 and Internet

From the book Website Creation. Website promotion. Earning money on the website of the author Max Anatoly

Uploading and uploading files via FTP Let's talk about how you can upload your files to remote server Internet so that others can then download them, and we will also consider another way to download files to your computer that does not involve using browsers and

From book Network tools Linux by Smith Roderick W.

Implementing the Strategy Pattern with Function Pointers The NVI idiom is an interesting alternative to public virtual functions, but it doesn't offer much from a design perspective. After all, we still use virtual functions For

From the Linux book: Complete Guide author Kolisnichenko Denis Nikolaevich

Implementing the “Strategy” pattern using the tr::function class If you are used to templates and their use to build implicit interfaces (see rule 41), then using function pointers will not seem like a very flexible solution. Why at all to calculate

From the book Fundamental Algorithms and Data Structures in Delphi by Bucknell Julian M.

Rule 38: Model the "contains" or "is realized by" relationship with composition Composition is a relationship between types that occurs when an object of one type contains objects of other types. For example: class Address (...); // residential addressclass

From the book Working on the Internet. Encyclopedia of the author Tashkov Petr Andreevich From the book Linux through the eyes of a hacker author Flenov Mikhail Evgenievich

Setting up a caching-only server B small networks often used DNS servers, whose main task is to cache the results of name resolution. This type of server does not support a specific domain (with the exception of the domain for the reverse

From the author's book

13.4.1. Configuring caching on the DNS server In order to enjoy this feature, you should add the following parameters to the options block of the named.conf file: forward first; forwarders ( 81.3.165.35; 81.3.150.2;); The forwarders directive specifies enclosed in braces list of IP addresses of DNS servers to which

From the author's book

Resolving Conflicts Using Linear Probing If the number of elements a hash table is likely to contain is known, you can allocate space for a hash table containing that number of elements and small number free cells "just in case"

From the author's book

Resolving Conflicts by Linking If we are willing to use additional cells beyond those required by the hash table itself, we can use another effective conflict resolution scheme, the closed addressing scheme. This method is called

From the author's book

Resolving Conflicts through Bucketing There is a variation of the bundling method for resolving conflicts called bucketing. Instead of putting a linked list in each cell, it puts a group in it, which essentially

From the author's book

Uploading and downloading files via FTP Let's look at how you can upload your files to a remote Internet server so that others can then download them, and we will also discuss another way to download files to your computer that does not involve using browsers and

Modern browsers quite often use a local cache in their work. What does this mean? This means that the browser, having received an HTML document, picture or other resource from the server, places it in its local cache (in other words, writes the received resource to HDD user machine) and during subsequent requests to such a resource does not access the server, but obtains the resource from the local cache.

This browser algorithm dramatically increases the loading speed of HTML documents. Since if the resource has already been loaded, and as a result is located in the local cache, then the access time is not determined throughput communication channel (for example, modem connection) and the speed work hard disk.

However, along with its advantages, this method also gives rise to a number of problems. In particular, most novice web programmers, when developing dynamic sites, face the same problem. The essence of this problem is that instead of repeatedly contacting the server for a page that runs a script on the server that modifies some information, the browser turns to the local cache. And as a result of, for example, three requests, there are not three modifications of the information located on the server, but only one.

In order to force the browser to request a page from the server every time, you need to prohibit the browser from entering this resource to cache. Below are the most common methods to disable or bypass caching.

Generating a new URL

Let's assume that the requested resource has the following url: test.html?id=7. As you can see from the url "and one parameter is passed to it. Let's add, for example, with JavaScript help, there is one more parameter in the url, and we’ll make it its value random number. As a result, the url will look like this: test.html?id=7&rnd=0.6700820127538827. Random parameter will be generated anew each time. Below is a listing demonstrating this approach:

Generating a new URL document.write("");

test link

Each time the result of such a request will be cached, but since caching is carried out over the entire url, a new url will be obtained each time and the browser will be forced to request a resource from the server, since the urls of the two requests will not match exactly.

Header fields You can also manage caching from the server side. To achieve this, the resource sent to the browser is accompanied by header fields. Detailed description

header fields can be found in the RFC 2068 standard, which describes the HTTP 1.1 protocol.

Expires header field

The value of this header is the date after which the contents of the resource will become obsolete. If a user accesses a resource after this date, the browser must request the resource from the server rather than from the local cache.< содержит дату, прошедшую, по отношению к текущей, то при следующем обращении к ресурсу браузер будет вынужден снова обратиться к серверу. Это произойдет вследствие того, что либо документ не будет занесен в кэш - как уже устаревший, либо при обращении к кэшу браузер определит, что документ уже устарел. Следующий листинг на PHP демонстрирует использование заголовка Expires:

If the field >Expires

Last-Modified header field The value of this header is date last update resource. Majority modern browsers

  • use the following algorithm if the resource is already in the local cache:
  • requests from the server the date of the last resource update
  • compares the received date and the resource date in the local cache

if the resource on the server is newer than the resource in the cache, the resource is requested from the server If a resource located on the server contains in this field current date

, then the browser will request the resource from the server each time, and not from the local cache. The following listing demonstrates the use of the Last-Modified header field:

Header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

And finally, the header fields, which are directly responsible for resource caching. The field was defined in the RFC 1945 standard describing the HTTP 1.0 protocol. This field is considered obsolete, but in some cases it is necessary to use it. In particular, some proxy servers do not correctly process requests for constantly changing resources if this header field is not sent along with the resource.

The second field is defined in the RFC 2068 standard, which describes the HTTP 1.1 protocol. This header field allows you to disable caching and request a resource from the server each time. The following listing demonstrates the use of the Cache-Control and Pragma header fields to disable caching:

Header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache");

How many times has it been written and rewritten about the tag... It would seem that’s it! Enough! Stop talk about this! But no! After all, not everything was said! Or rather, everything is here and there, but try to find it again. Try all sorts of Rambler's and Yandex's...

So let's talk about caching. Actually, why do we all need it, poor Internet users, as a habitat. Reducing traffic - that's the benefit! Well, why do we need to re-download the first page of some search directory from the Internet ten times a day, when we can carefully put it in the cache and forget about the slow loading for this site? That's exactly what they do. Let's delve into the problem.

Everyone knows that different versions HTTP protocols have their own caching control directives. Cache-Control is a directive of the HTTP/1.1 protocol. And her parameters are like this:

All listed attributes (except for mutually exclusive ones) can be listed separated by commas. I understand that not everything is clear, now I’ll explain something.

1. Ban caching at all (the document will not be cached by either the proxy server or the browser):

2. The document will be cached by the browser, but will not be cached by the proxy server.

3. The document will be cached, even if it shouldn’t, it seems, under normal circumstances.

4. The document is cached, but not saved in the archive.

5. You can directly tell the browser: “Refresh this page for me.” (The max-age parameter specifies how many seconds the document is cached.) Can be useful when using PHP to programmatically update pages.

6.And you can only say this to the proxy server.

As a legacy of the HTTP 1.0 protocol, we inherited a very simple way to manage caching, determined by the Pragma directive. This thing is a general HTTP message header directive in HTTP/1.0, and has no other meanings other than no-cahce:

IN HTTP protocol 1.1 this directive is replaced by the Cache-Control directive with the value no-cache. Most servers and clients support this directive and handle it correctly.

To disable caching, sometimes it is not enough to use caching control directives. This is how Netscape caches documents or their components even in the presence of Cache-Control and Pragma directives. In order to force the page component to be reread (after all, it is received from the server via an independent HTTP request), you can set the Expires directive.

This is how the conversation about caching turned out. And we will meet again with the META tag. And more than once...

Good bad

    Meta tags are optional attributes of a page placed in its header that contain a description of the page, keywords to her, some information about...