This website stores cookies. Click here to accept them.cookie information page

"php" category

all about caching

31st December 2011
Categories: caching, php, programming

Caching is when the web server, the browser, or even an intermediate server store a copy of an image, javascript file, css file, or html page, in order to prevent the browser from putting too big a load on the web server by requesting the same page multiple times - for example if you're browsing around a site and frequently return to the home page, or if all of the pages use the same css stylesheet.

So that's a good thing, right?

Unfortunately caching can inconvenience the web developer quite a bit. Take for instance, a website with a little "your basket" sign in the top right corner, informing the user that the basket contains 1 item. When the user hits the home page again, they won't particulalry understand if the sign now says "nothing in your basket". Or take the developer who's designed a makeover for a website, changing the css. They don't want users who are frequent visitors to come back to the site and get a mishmash of content and styles because the css won't update automatically.

Fortunately there are ways around caching problems, and ways to give developers control over caching important elements of a page without caching everything.

Within the page HTML, if you don't want the browser to cache the page, you can add a meta-tag in the page header:


While the browser understands this, however, an additional HTTP header is required in order to make sure that intermediate servers understand the requirement not to cache. In PHP:


header("Cache-Control: no-cache, no-store");
header("Pragma: no-cache");


In Classic ASP:


Response.Expires= -1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "no-cache"


This doesn't solve the problem of cached css. Luckily, there is a very simple solution when you've just made changes to the css:

<link rel="stylesheet" href="style.css?version=2" type="text/css">

This stylesheet reference says "style.css?version=2" instead of just "style.css".  The browser has to assume that these two files are different, although if you haven't done anything fancy with intercepting calls to css files, they are in fact exactly the same file.  Once the browser has grabbed the latest file off the server by requesting style.css?version=2 then it has that file, and won't need a new one until you change the version number in your html reference to it.

But what about the benefits of caching?

We seem to have overturned the point of caching, at least with PHP pages, so how do we make up for it? My answer is to cache specific portions of the page, i.e. the ones that don't have to be up-to-the-second, and may involve a complicated set of SQL queries to get the information out. You can turn to a commercial caching solution for this, or create a cached content table which you manage yourself. Bear in mind that this will grow and need to be tidied regularly. The code below doesn't supply the housekeeping solution.

First, write the function that gets all the content. Then, when you want to write out the content, instead of calling that function, see if a cache exists. Use some unique code for your content, and call this function:

static function GetCache($UniqueRef, $MinsToCache=3000)


$sql='select cachevalue,lastcached from cachetable where   uniqueref='.MySQuote($UniqueRef);




return false;





return false;


return $aRow['cachevalue'];




If the function returns nothing, call the content-generator function to get the content, and before outputting, cache it - something like:


static function DoSomeCaching($UniqueRef, $CacheValue)



$sql='UPDATE cachetable ';

$setsql=' SET uniqueref='.MySQuote($UniqueRef);


$setsql.=',lastcached='.MySQuote(date('Y-m-d H:i'));

$sql.=$setsql.' WHERE uniqueref='.MySQuote($UniqueRef);





$sql=' INSERT INTO cachetable '.$setsql;




The MySQuote() function is a mysql single quote escaping function - replace calls to this with your preferred method.

www and non-www

28th December 2011
Categories: php, seo

While most websites serve up exactly the same web pages on the www and non www versions ( and it is good practice to have one 'canonical' version of your website and divert the other version to it, as search engines have traditionally viewed the two versions as duplicates (which isn't good) and having one route to a 'resource' narrows down the margin of error if there are changes to the way you serve up web pages.

In php you can divert one to the other using the following code:

// redirect to www if not there already
  && (strpos($_SERVER['HTTP_HOST'],'')!==false)
  Header( "HTTP/1.1 301 Moved Permanently" );

If you have access to the DNS for your website you may wish to point the non-www site at a different location altogether, with just one index.php file containing the following code:

Header( "HTTP/1.1 301 Moved Permanently" );

Note that the header containing the 301 code is important for search engines; in almost all cases you should use a 301 (permanent) redirect as it tells search engine spiders that the old url is redundant and the new url should be credited with any 'kudos' that the old url ever had.

A slightly neater way of doing the redirect is to do it outside of PHP code, in your .htaccess file if you are running on apache and you have permissions (via the apache config) to do it:

# rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# non www to www
RewriteCond %{HTTP_HOST} !^www\.mygreatwebsite\.org$
RewriteRule (.*)$1 [R=301,L]

On IIS you can do it using the website properties within IIS manager.  Just add another website for the non-www version, and once added, in the website properties, instead of serving web pages from a folder on your computer, choose to make it a 'redirection to another URL'.  Remember to take the non-www version off the host headers accepted by the www version (website... advanced button).

What are PHP, ASP and Java?

1st December 2011
Categories: basics, php

What are PHP, ASP and Java?

Having read the previous posts, you'd be forgiven for thinking that the world wide web is just a collection of interlinking documents.

That's how it started, of course, but now we have much more than that.

What links the static documents stage to a more interesting world wide web, where people can send and receive information, buy products, and generally interact with the web pages they visit, is software especially developed for building web pages.

There are three main contenders in this area:

- PHP (An open source system which stands, confusingly, for PHP Hypertext Preprocessor)

- ASP (Microsoft's 'Active Server Pages', which support various languages, including C# and Visual Basic)

- Server-side Java (another open source system originally developed by Sun) 

I'm calling them "systems" rather than "languages" because getting them working is a bit more complicated than writing software and sticking it in a compiler... but that's too much jargon already, please bear with me...

My aim in this article is to explain what these systems do that HTML doesn't, so rather than delve into the individual languages, I'll write some pseudo-code (pretend software code) to explain.

Say, for example, that you wanted the user to tell you their name. In HTML you can ask for their name - the code would look something like this:

<form> What's your name? <input type="text" name="namebox"> ... </form>

But HTML has no way of capturing the name once the user has typed it in, so we need a programming language so do something like this:

User_name = Get_the_user_input("namebox")
Print "Hello " + User_name

User_name is what's called a variable, in other words a placeholder for a value that can be changed at any time.

Get_the_user_input is pseudocode (pretend software code) for however you would retrieve some user input in your language of choice.

Of course, PHP, ASP and Java can do a lot more that retrieve input. They can store information, manipulate information, and output information. And like most computer languages they can make decisions and go around loops. This gives them the ability to do pretty much anything you want a website to do, given careful planning and the time to code.

All three also have the ability to create and manipulate "objects", a special kind of variable designed to mimic objects in the real world, which can have multiple properties and functions, and so can speed up the process of programming... but more on that in another post!

© Alberon Ltd 2019

8 Standingford House
26 Cave Street

01865 596 144

Oxford Web is a trading name of Alberon Ltd, registered company no. 5765707 (England & Wales).