<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Developer</title>
	<atom:link href="http://www.phpdeveloper.org.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloper.org.uk</link>
	<description>Freelance PHP Developer based in Manchester, UK</description>
	<lastBuildDate>Sat, 21 Jan 2012 14:41:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Top PHP security blunders</title>
		<link>http://www.phpdeveloper.org.uk/top-php-security-blunders/</link>
		<comments>http://www.phpdeveloper.org.uk/top-php-security-blunders/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 14:41:47 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=544</guid>
		<description><![CDATA[Whilst looking through my archive of technology blog posts, I came across this article on SitePoint: Top 7 PHP Security Blunders. Although written in 2005, most of the points made are still relevant today, and it is well worth a &#8230; <a href="http://www.phpdeveloper.org.uk/top-php-security-blunders/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst looking through my archive of technology blog posts, I came across this article on SitePoint: <a href="http://www.sitepoint.com/php-security-blunders/">Top 7 PHP Security Blunders</a>. Although written in 2005, most of the points made are still relevant today, and it is well worth a read as a general overview of potential security weaknesses in your PHP code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/top-php-security-blunders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using WordPress as a Content Management System</title>
		<link>http://www.phpdeveloper.org.uk/using-wordpress-as-a-content-management-system/</link>
		<comments>http://www.phpdeveloper.org.uk/using-wordpress-as-a-content-management-system/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 14:04:19 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=508</guid>
		<description><![CDATA[This is an archived post from my old technology blog, which I&#8217;ve imported here as it relates to PHP. It was originally published on 02/08/2008. Things To Consider When Using WordPress as a CMS (via: Matt) I&#8217;ve been looking for a &#8230; <a href="http://www.phpdeveloper.org.uk/using-wordpress-as-a-content-management-system/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This is an archived post from my old technology blog, which I&#8217;ve imported here as it relates to PHP. It was originally published on 02/08/2008.</em></p>
<p><a href="http://www.devlounge.net/publishing/things-to-consider-when-using-wordpress-as-a-cms">Things To Consider When Using WordPress as a CMS</a> (via: <a href="http://ma.tt/2008/07/using-wordpress-as-a-cms/">Matt</a>)</p>
<p>I&#8217;ve been looking for a half-decent content management system for some time, as until now I&#8217;ve been writing my own code for each site that I run. This is a real pain, as I never have the time to write an admin interface &#8212; and therefore end up using phpMyAdmin &#8212; and I&#8217;m not as good as designing interfaces as other people. However, I have thought several times about using WordPress as a content management system &#8212; even though it started as a blogging platform &#8212; which is why the above article is particularly of interest to me.</p>
<p>Reasons why I would consider using WordPress as a CMS:</p>
<ul>
<li> <strong>Spam protection:</strong> <a href="http://www.akismet.com/">Akismet</a> is by far the most effective anti-spam solution I&#8217;ve come across, it probably performs better on blog comments than SpamAssassin does on my email. Sure, there are <a href="http://akismet.com/development/">plugins available for several other systems</a>, but with WordPress the support is practically built-in. Yes, it is technically a plugin, but it comes shipped with the distribution, so there&#8217;s one less step to mess around with.</li>
<li><strong>Familiarity:</strong> I use WordPress on several other blogs, so I&#8217;m comfortable with the interface and the way the software does things. In a similar vein, readers of my sites/blogs are also more likely to be familiar with the commenting interface for WordPress than for other content management systems</li>
<li><strong>Theme availability:</strong> WordPress seems to have more themes available for it than any other blog or content management system I&#8217;ve seen. You can download thousands of free themes, pay less than $100 for an off-the-shelf professionally designed theme, or cough up for a designer to create a unique theme especially for your site &#8212; there are plenty of companies and individuals around who offer this service. What&#8217;s more, WordPress themes can make your site look unique &#8212; one of my major bugbears with <a href="http://drupal.org/">Drupal</a> is that all of the themes tend to look like Drupal sites.</li>
<li><strong>Plugins:</strong> Like themes, there are thousands of plugins available, enabling pretty much anything you could possibly want to do with WordPress &#8212; from formatting posts with Textile to cross-posting to other sites (e.g. LiveJournal) automatically. I&#8217;m not aware of any other system with the same variety of plugins (though Drupal probably comes a close second), and I suspect this is largely due to the ease of creating and installing plugins for WordPress.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/using-wordpress-as-a-content-management-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using exec to reduce PHP memory use</title>
		<link>http://www.phpdeveloper.org.uk/using-exec-to-reduce-php-memory-use/</link>
		<comments>http://www.phpdeveloper.org.uk/using-exec-to-reduce-php-memory-use/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 18:20:54 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[PDF]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=473</guid>
		<description><![CDATA[As mentioned in a previous post, I&#8217;ve recently started to use a library called dompdf to convert HTML to PDF using PHP. One of the major problems I came across was the amount of memory used by this library, which &#8230; <a href="http://www.phpdeveloper.org.uk/using-exec-to-reduce-php-memory-use/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As mentioned in a previous post, I&#8217;ve recently started to use a library called dompdf to convert HTML to PDF using PHP. One of the major problems I came across was the amount of memory used by this library, which was over 16MB for some files. Initially I generated each file using a call to a function with arguments which described the type of PHP file I wanted to create. The function broadly worked as such:</p>
<ol>
<li>Select information from database.</li>
<li>Assign information to Smarty template.</li>
<li>Parse Smarty template to produce a string of HTML.</li>
<li>Create a dompdf object with the HTML string as its input.</li>
<li>Generate the PDF and save the file to disk.</li>
<li>Exit the function.</li>
</ol>
<p>I erroneously assumed that PHP would perform some garbage collection when exiting the function (i.e. when the object fell out of scope), thus freeing up the memory used by the dompdf object. Unfortunately, PHP didn&#8217;t do this, resulting in the script using more and more memory each time I called the function, which eventually broke through the limit set for individual PHP scripts and so execution was halted. As the PDF files differed in size, I couldn&#8217;t guarantee when this problem would happen and allow for it.</p>
<p>In order to get round the memory use problem, I tried using the <a href="http://www.php.net/unset">unset</a> function to free up the memory used by the dompdf object. However, unset merely removes the reference to an object, and does not force the garbage collector to free up the memory immediately. As a result, the memory barrier was still being broken at an undetermined point.</p>
<p>Finally, I came across a somewhat forceful but successful method of getting around the problem. Instead of putting the PDF generation code in a function, I moved it into a separate PHP file and then executed this file within my main script using the <a href="http://www.php.net/exec">exec</a> function like so:</p>
<pre>exec('/usr/bin/php /path/to/GeneratePDF.php');</pre>
<p>Instead of using 16MB for each function call and not freeing the memory, therefore using 160MB for 10 calls, PHP freed up all the memory used by the separate script when it finished executing, so at any one point there was never more than 16MB in total being used. I benchmarked both methods (function calls and exec) by trying to create a number of PDF files, and the results were quite impressive: using the function calls took over 60 seconds and several of the files failed, using <code>exec</code> took 30-40 seconds and every file was generated successfully.</p>
<p>Normally I would not recommend using functions which execute external programs, due to the security implications involved. However, in this case no user-supplied data is passed to <code>exec</code>, and the huge improvement in speed and memory use makes it a no-brainer for this particular example.</p>
<h3>Further information</h3>
<ul>
<li><a href="http://groups.google.com/group/dompdf/browse_thread/thread/c253a52b10766a33">Batch processing slows down PDF generation</a> &#8212; mailing list thread on Google Groups.</li>
<li><a href="http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29">Garbage collection</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/using-exec-to-reduce-php-memory-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating PDF files in PHP with Smarty and dompdf</title>
		<link>http://www.phpdeveloper.org.uk/creating-pdf-files-in-php-with-smarty-and-dompdf/</link>
		<comments>http://www.phpdeveloper.org.uk/creating-pdf-files-in-php-with-smarty-and-dompdf/#comments</comments>
		<pubDate>Sat, 17 Dec 2011 18:05:20 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=462</guid>
		<description><![CDATA[Back in 2010 I&#8217;d been looking for a way to generate PDF files dynamically on a Web site mainly to create files which can be printed or emailed and will work on any computer (printer-friendly pages with CSS didn&#8217;t quite &#8230; <a href="http://www.phpdeveloper.org.uk/creating-pdf-files-in-php-with-smarty-and-dompdf/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Back in 2010 I&#8217;d been looking for a way to generate PDF files dynamically on a Web site mainly to create files which can be printed or emailed and will work on any computer (printer-friendly pages with CSS didn&#8217;t quite achieve the effect I wanted). The requirements included:</p>
<ol>
<li>A sensible layout system which would automatically put elements in the correct place &#8212; I don&#8217;t want to have to manually specify where the text should begin on the page, align each paragraph, trigger a new page etc.</li>
<li>The ability to transform HTML into PDF, so that I can use Smarty to produce the PDF files as well as the Web pages.</li>
<li>Support for as much CSS as possible.</li>
<li>Active mailing list or other method of getting help from the community.</li>
<li>Software under active development, i.e. not one of these Sourceforge projects which were started in 2005 and haven&#8217;t been updated since.</li>
<li>Uses a licence which enables the library to be integrated with closed-source software (e.g. BSD or LGPL).</li>
</ol>
<p>There are several HTML to PDF tools out there, but most of them are not up to the task. <a href="http://www.htmldoc.org">HTMLDOC</a> looks promising at first, until you realise that it only supports part of HTML 4 and doesn&#8217;t support CSS at all &#8212; at least not according to the FAQ. <a href="http://www.reportlab.org/">ReportLab</a> also raised my hopes, despite being written in Python (which I&#8217;m not familiar with), but a markup language is only available with the commercial version. <a href="http://sourceforge.net/projects/html2fpdf/">HTML 2 PDF</a> didn&#8217;t even get off the drawing board by the looks of things, and hasn&#8217;t been updated since 2005. Finally I came across <a href="http://www.dompdf.com">dompdf</a>, which seems to fit the bill with the following features:</p>
<ol>
<li>Supports conversion from HTML to PDF.</li>
<li>Support for a large chunk of CSS (imperfect but improving).</li>
<li>Active mailing list, which includes the developers of the software.</li>
<li>Licensed under the LGPL.</li>
</ol>
<p>Using dompdf at its most basic is a doddle, you simply pass the HTML in as a parameter to the <code>load_html</code> function and choose whether you wish to stream the result to the browser or output to a file. The bare minimum code is shown below:</p>
<pre>$html = $template-&gt;fetch('template.tpl');
$filename = '/path/to/file.pdf';

$dompdf = new DOMPDF();
$dompdf-&gt;load_html($html);
$dompdf-&gt;set_paper('a4', 'portrait');
$dompdf-&gt;render();
file_put_contents($filename, $dompdf-&gt;output());</pre>
<p>Assuming <code>$smarty</code> is a reference to an existing Smarty template, this will create a PDF which should look more or less the same as the Web page generated by <code>$smarty-&gt;display('template.tpl')</code>. There are other options which allow you greater control over the final PDF, such as altering the page size, loading extra fonts etc., but the above code will produce a working PDF which you can email or print. The size of the PDF is also extremely small &#8212; the invoices which I have working on come in at around 2-3KB each.</p>
<p>Things to watch out for include:</p>
<ol>
<li>File permissions &#8212; as you&#8217;re saving files to disk the Web server user (www-data if you&#8217;re running Apache on Debian) will need write access to the relevant directory.</li>
<li>Databases &#8212; do <strong>not</strong> store the files in a database. By all means store the meta data, such as filename, last modified time etc., in a database (I do this for ease of management), but don&#8217;t store the file itself. I&#8217;ll write another post at a later date detailing why storing files in a database is a bad idea.</li>
<li>Unicode &#8212; unfortunately dompdf doesn&#8217;t have full Unicode support yet, so if you want to create documents with this character set you will have to wait a while. I believe it&#8217;s possible to make dompdf work with Unicode by using the commercial version of pdflib, but I haven&#8217;t tried that myself.</li>
</ol>
<p>dompdf isn&#8217;t perfect of course, it takes a long time to generate PDF files with tables, some CSS rules don&#8217;t work and ordered lists are currently unsupported. However, it is under active development, and there have been performance improvements in recent versions. At the moment I have to generate PDFs as part of a cron job instead of on the fly, and employ a bit of a hack to get round memory usage problems, but I expect those problems to gradually diminish over time. Even with these minor blemishes, dompdf is still the best library I&#8217;ve found for converting HTML to PDF in PHP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/creating-pdf-files-in-php-with-smarty-and-dompdf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Downloading files in Internet Explorer over SSL</title>
		<link>http://www.phpdeveloper.org.uk/downloading-files-in-internet-explorer-over-ssl/</link>
		<comments>http://www.phpdeveloper.org.uk/downloading-files-in-internet-explorer-over-ssl/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 18:03:16 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=459</guid>
		<description><![CDATA[There&#8217;s a known problem with Internet Explorer when it tries to download files on a secure site (i.e. one with a https prefix). For some reason, IE tries to save the file as an attachment, instead of displaying it in &#8230; <a href="http://www.phpdeveloper.org.uk/downloading-files-in-internet-explorer-over-ssl/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a known problem with Internet Explorer when it tries to download files on a secure site (i.e. one with a https prefix). For some reason, IE tries to save the file as an attachment, instead of displaying it in the browser (e.g. an image or PDF). You can fix this issue by altering some of your browser settings, which is fine for an individual user but obviously doesn&#8217;t work if you run a website and want users with Internet Explorer to be able to download files from it. The issue appears to affect even the latest version of IE, so you cannot just ignore it in the hope that everyone has upgraded from IE 6 now.</p>
<p>I encountered this issue on a project I was working on, and after many attempts using trial and error I found that you need to send the following headers before the file is downloaded:</p>
<pre>Cache-control: private
Content-Type: application/pdf
Pragma: public</pre>
<p>I don&#8217;t think the order matters, but the sequence above works for me and I haven&#8217;t tried moving the headers around. Of course, you should replace <code>application/pdf</code> with the content type for whatever file you are sending to the browser.</p>
<p>This fix does not affect other browsers, which all seem to download the file perfectly well without the headers, nor does it break downloads on non-SSL sites. I don&#8217;t know why it works, but I&#8217;m posting it online in the hope that other people who are encountering the same problem won&#8217;t have to waste half a day trying to fix it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/downloading-files-in-internet-explorer-over-ssl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UK Company Data library</title>
		<link>http://www.phpdeveloper.org.uk/uk-company-data-library/</link>
		<comments>http://www.phpdeveloper.org.uk/uk-company-data-library/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 22:57:39 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Libraries]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=432</guid>
		<description><![CDATA[As part of the work for Filing Reminders, I needed to fetch company data (specifically filing dates) from data.companieshouse.gov.uk. In order to do this, I wrote a small wrapper class which normalises company numbers (e.g. 123456 becomes 00123456, as numbers &#8230; <a href="http://www.phpdeveloper.org.uk/uk-company-data-library/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As part of the work for <a href="http://www.filingreminders.co.uk">Filing Reminders</a>, I needed to fetch company data (specifically filing dates) from data.companieshouse.gov.uk. In order to do this, I wrote a small wrapper class which normalises company numbers (e.g. 123456 becomes 00123456, as numbers are fixed at eight characters in length) and makes a request for that company&#8217;s details in JSON. It&#8217;s not a particularly complicated piece of code, but on the off-chance that it could be of use to someone else, I&#8217;ve made it available as <a href="https://github.com/pwaring/ukcd-php">ukcd-php</a> under <a href="https://github.com/pwaring">my GitHub account</a>. The licence for the library is MIT, so you shouldn&#8217;t have any problems incorporating it in closed source projects, though please feel free to send in bug reports, feature requests or improvements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/uk-company-data-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filing Reminders launched</title>
		<link>http://www.phpdeveloper.org.uk/filing-reminders-launched/</link>
		<comments>http://www.phpdeveloper.org.uk/filing-reminders-launched/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 17:00:54 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=412</guid>
		<description><![CDATA[I&#8217;ve recently launched Filing Reminders, a free email service to remind you when the filing dates for a UK company are approaching. The design comes from the highly useful Bootstrap, which allows me to put together a basic layout and &#8230; <a href="http://www.phpdeveloper.org.uk/filing-reminders-launched/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently launched <a href="http://www.filingreminders.co.uk">Filing Reminders</a>, a free email service to remind you when the filing dates for a UK company are approaching. The design comes from the highly useful <a href="http://twitter.github.com/bootstrap/">Bootstrap</a>, which allows me to put together a basic layout and get a site launched, rather than delaying whilst I work on the CSS/HTML.</p>
<p>The primary reason for building the site was to scratch my own itch. Over the past few years, I&#8217;ve been a director of five different companies, each of which has two filing dates (accounts and return) to remember. Whilst I could just enter the dates in my diary each year, I wanted an automated service which would nag me by email until the documents were filed.</p>
<p>The site is completely free to use and you can subscribe to as many companies as you want. At some point in the future I may charge for premium services, or include a sponsor link in the emails, but the basic email service will remain free.</p>
<p>In terms of the technical details, Filing Reminders is written in PHP, uses MySQL for data storage and my <a href="https://github.com/pwaring/ukcd-php">UKCD class</a> to fetch the company data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/filing-reminders-launched/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Curry Gateway launched</title>
		<link>http://www.phpdeveloper.org.uk/curry-gateway-launched/</link>
		<comments>http://www.phpdeveloper.org.uk/curry-gateway-launched/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 09:00:13 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=419</guid>
		<description><![CDATA[I&#8217;ve recently launched Curry Gateway, a directory of curry houses in the British Isles which, like Pub Gateway, focuses on precise geodata and eschews intrusive advertising. Most of the curry houses have been added as a result of my visits &#8230; <a href="http://www.phpdeveloper.org.uk/curry-gateway-launched/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently launched <a href="http://www.currygateway.co.uk">Curry Gateway</a>, a directory of curry houses in the British Isles which, like <a href="http://www.pubgateway.co.uk">Pub Gateway</a>, focuses on precise geodata and eschews intrusive advertising. Most of the curry houses have been added as a result of my visits to them over the years, although I will soon be adding the ability for other visitors to submit their local restaurant or takeaway.</p>
<p>For anyone interested in the technical details, Curry Gateway is written in PHP and uses MySQL for the data storage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/curry-gateway-launched/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serving static files from a different domain</title>
		<link>http://www.phpdeveloper.org.uk/serving-static-files-from-a-different-domain/</link>
		<comments>http://www.phpdeveloper.org.uk/serving-static-files-from-a-different-domain/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 17:21:53 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=380</guid>
		<description><![CDATA[Whilst debugging a site last week, I noticed the following header was being sent to the server with each request (actual name/value removed): Cookie: cookiename=cookievalue When requesting the main PHP page this is a necessary header, because PHP uses it &#8230; <a href="http://www.phpdeveloper.org.uk/serving-static-files-from-a-different-domain/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Whilst debugging a site last week, I noticed the following header was being sent to the server with each request (actual name/value removed):</p>
<pre>Cookie: cookiename=cookievalue</pre>
<p>When requesting the main PHP page this is a necessary header, because PHP uses it to populate the $_COOKIE array, which is in turn used to check whether a user is logged in to the site. However, with any other files, such as stylesheets and images, this header is ignored, unless you want to ensure that images are only available to authenticated users (you could do this by redirecting any image file requests to a script which only serves the image if the user is logged in).</p>
<p>It might not seem like a big deal, but if we assume that the length of the header is 50 characters, each character is one byte, and we have ten static elements on a page, that&#8217;s 500 bytes of unnecessary traffic. In fact, s6.1 of <a href="http://tools.ietf.org/html/rfc6265">RFC 6265</a> suggests that browsers should support cookies at least 4096 bytes in length, so in theory you could be unnecessarily transferring four kilobytes with each request for a static file, especially if you use several cookies. This quickly adds up if your users are on slow or unreliable connections, such as dial-up or wireless links.</p>
<p>A simple way to get around this problem is by placing all static files on a different subdomain (e.g. static.example.org), or a completely separate domain (e.g. www.example.net). Both options involve a penalty of an additional DNS lookup when the first page is accessed, and have slightly different effects.</p>
<p>With a separate subdomain, the Cookie header will still be sent if the domain of the cookie is set to .example.org (note the leading &#8216;.&#8217;), as this defines the cookie as being valid across all subdomains of example.org. This might be the case if you have different subdomains for parts of your site, such as forum.example.org, admin.example.org etc.</p>
<p>With a completely different domain, the Cookie header will not be sent, but you do have to incur the cost of registering and managing another domain.</p>
<p>As well as the Cookie header overhead, some proxies may fail to cache images correctly because the Cookie header suggests there is some state associated with the files, even though HTTP is supposed to be a stateless protocol.</p>
<p>So, if you have a lot of static files loaded for each page on a domain which uses cookies, it may be worthwhile running some benchmarks to see how much additional data you are transferring as a result, and whether the overhead of an additional DNS lookup is an acceptable trade off.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/serving-static-files-from-a-different-domain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If PHP Were British</title>
		<link>http://www.phpdeveloper.org.uk/if-php-were-british/</link>
		<comments>http://www.phpdeveloper.org.uk/if-php-were-british/#comments</comments>
		<pubDate>Sun, 28 Aug 2011 17:20:31 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.phpdeveloper.org.uk/?p=378</guid>
		<description><![CDATA[Forwarded to me by a fellow PHP developer: If PHP Were British.]]></description>
			<content:encoded><![CDATA[<p>Forwarded to me by a fellow PHP developer: <a href="http://www.addedbytes.com/blog/if-php-were-british/" target="_blank">If PHP Were British</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloper.org.uk/if-php-were-british/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

