Testimonials  >

Many thanks to the company ControlStyle for successful implementation of the project on table tennis http://tennis-table.com.ua . Today it is very difficult to find the company, having the executive team of professionals and associates, like...

TENNIS-TABLE,
tennis-table.com.ua

Articles  >

Articles  >  Programming  >  If-Modified-Since, when PHP is not installed as an Apache module

Each web-programmer which is interested caching of web-pages on the client-side, knows about such headers HTTP, as "If-Modified-Since" and "If-None-Match". This headers are sent by a browser at the request a page which is present in it cache. For the correct organization of caching on the client-side, it is necessary for server application to send header "HTTP/1.0 304 Not Modified" and to stop data transfer in case contents of required page have not changed since that moment of time which is specified in the sent header "If-Modified-Since".

The basic problem at realization of caching on the client-side consists in accessing to contents of header "If-Modified-Since". It is by that by default specified header is available from the server application only in the event that interpreter PHP is installed as module Apache that happens extremely seldom on servers of the organizations offering services of a hosting (for secuirity reasons and convenience of recompilation PHP). It is necessary to note, that client-side caching salutarily influences not only loading of the web-server, but also speed of indexation of a website by search engines. In this connection, skilled SEO-experts persistently search and recommend "correct" hosting companies.

Actually, there is the universal decision of the that problem not demanding intervention in a global configuration of the web-server and working even in that case when PHP it is not installed as module Apache. For using of that method it is necessary and enough that the following conditions satisfying:

  • possibility of a configuration through files .htaccess;
  • availability of a module mod_rewrite;
  • in the server application it is necessary to get values of headers "If-Modified-Since" and "If-None-Match" through an array $ _SERVER, instead of by means of functions getallheaders or apache_request_headers (these functions are available only when PHP it is installed as module Apache).

So, for realization of access to headers "If-Modified-Since" and "If-None-Match" it is necessary to place in the root of a website a file .htaccess the following contents:

RewriteEngine On
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

After that, necessary headings will be available as $ _SERVER ['HTTP_IF_MODIFIED_SINCE'] and $ _SERVER ['HTTP_IF_NONE_MATCH']. When on a website mod_rewrite it is used for generation "beautiful" URL, contents of .htaccess will become:

# url rewriting
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA]

# If-Modified-Since (if php is not installed as cgi then comment lines below)
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

It is necessary to note, that headers "If-Modified-Since" and "If-None-Match" are not sent by a browser if in the previous requests to this page it did not receive header "Last-Modified" in the answer of the web-server. Besides the specified headers also will not be sent by a browser at use in the web-application of sessions with configuration by default. It is necessary to execute before start of session function session_cache_limiter, passing as argument parameter 'private_no_expire' to avoid such behaviour of a browser:

<?php

session_cache_limiter('private_no_expire');
session_start();

?>

← To publications list

Nikolay I. Yarovoy,
04/22/2006.

Last projects:  Contact lens, Ekaterinburg

Back to top© 2019 ControlStyle, web site development. All rights reserved.
Web site promotion and advertising.