<?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>Autarchy of the Private Cavehttp &#187;</title> <atom:link href="http://bogdan.org.ua/tags/http/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Tue, 15 May 2012 21:56:55 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>HTTP caching: universal approach and sample code</title><link>http://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html</link> <comments>http://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html#comments</comments> <pubDate>Sat, 09 Dec 2006 09:16:53 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[caching]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[http]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html</guid> <description><![CDATA[As described in my previous post, there are some rather simple mechanisms to enable visitor's browser to cache content, and avoid unnecessary load on your servers. In this post I'll take a look at some parts of the practical implementation of the caching mechanism on the server, using PHP. I think of caching as a [...]]]></description> <content:encoded><![CDATA[<p>As described in my <a
href="http://bogdan.org.ua/2006/10/18/http-caching-request-and-response-headers.html" title="HTTP caching: request and response headers">previous post</a>, there are some rather simple mechanisms to enable visitor's browser to cache content, and avoid unnecessary load on your servers. In this post I'll take a look at some parts of the practical implementation of the caching mechanism on the server, using PHP.<br
/> <span
id="more-50"></span><br
/> I think of caching as a set of relatively independent procedures:</p><ol><li>Process browser's request - find out, if the browser is asking for content for the first time, or is looking for an updated version. If the browser's content is fresh, just reply that it's fresh. Otherwise, proceed to step 2. Also proceed to step 2, if this is a first-time request.</li><li>If this is a first-time request, check if we have a fresh locally cached content, and send it in response, to bypass <em>de novo</em> contet generation. If local cache is stale, proceed to step 3.</li><li>Generate new (fresh) content cache item, and send it to browser, together with cache-enabling headers.</li></ol><p>I decided to put all the header-related processing into one function, and all the cache-creation and sending - into another. If you prefer, you can make an object with two or more methods, but I see no need in doing so.</p><p>Here's the headers part:</p><div
class="igBar"><span
id="lphp-4"><a
href="#" onclick="javascript:showPlainTxt('php-4'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-4"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> HTTP_caching<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$timestamp</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#008000;">/*</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * HTTP_caching(v0.3)</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * checks headers for eTag or Modified request,</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * and replies with HTTP 304 Not Modified</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * if $timestamp is equal to the requested time/tag</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * returns TRUE if 304, FALSE otherwise</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp;&nbsp; * produces Last-Modified and ETag headers from $timestamp</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">&nbsp; &nbsp; */</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$latest</span> = <a
href="http://www.php.net/gmdate"><span
style="color:#000066;">gmdate</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">'D, d M Y H:i:s'</span>, <span
style="color:#0000FF;">$timestamp</span><span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">' GMT'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$etag</span> = <a
href="http://www.php.net/md5"><span
style="color:#000066;">md5</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$latest</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$not</span> = <span
style="color:#000000; font-weight:bold;">false</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/isset"><span
style="color:#000066;">isset</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$_SERVER</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'HTTP_IF_MODIFIED_SINCE'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$_SERVER</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'HTTP_IF_MODIFIED_SINCE'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#0000FF;">$latest</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"HTTP/1.1 304 Not Modified"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Status: 304 Not Modified"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#0000FF;">$not</span> = <span
style="color:#000000; font-weight:bold;">true</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#616100;">elseif</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <a
href="http://www.php.net/isset"><span
style="color:#000066;">isset</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$_SERVER</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'HTTP_IF_NONE_MATCH'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$_SERVER</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'HTTP_IF_NONE_MATCH'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#0000FF;">$etag</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"HTTP/1.1 304 Not Modified"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Status: 304 Not Modified"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#0000FF;">$not</span> = <span
style="color:#000000; font-weight:bold;">true</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span>!<span
style="color:#0000FF;">$not</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Last-Modified: "</span> . <span
style="color:#0000FF;">$latest</span> <span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"ETag: "</span> . <span
style="color:#0000FF;">$etag</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#616100;">return</span> <span
style="color:#0000FF;">$not</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><p>This function checks two caching-related request headers: If-None-Match and If-Modified-Since. Use of two headers is redundant, but this was done with an idea to provide different caching methods for different content types in the future. Timestamp-based approach should be sufficient in most cases, but having ETag as well does not hurt.</p><p>The parameter this function takes - $timestamp - is the date of the locally cached content item. We should check if the cached item exists before calling HTTP_caching; and if cached item does not exist, we should create it and then call HTTP_caching with the timestamp of the newly generated cached content item.</p><p>Function is utterly simple, but needs some explaining comments:</p><ul><li>There is a convenient function "apache_request_headers", but as its name implies, it works only under apache. Thus I had to use global PHP variables for fetching specific request headers. However, this approach also doesn't work on all hosting providers - you just do not get those headers, though they are sent by the browser. But you still benefit from local caching, and the only drawback of being unable to process request headers is that you waste traffic sending data which could have been unsent.</li><li>I'm also sending two equal headers - standard HTTP/1.1, and outdated Status header. This is redundant (sending Status might be removed), but depending on how your PHP is installed (as apache-httpd module or CGI) you may want to leave both in place, or experiment and leave the one which actually works.</li></ul><p>Now, let's move on to actual caching. The simplest and quite reliable method of identifying any object within your cache is md5(url) - that is, the hash of the request URL. Note, that you might want to hash not the complete URL (starting with http://), but only the part after the TLD's slash, e.g. for complete URL http://bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html you would hash only the "xnameorg-down-largest-ddos-they-ever-had.html" part (or "2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html", if the filename part of the path might be non-unique). Evidently, this will save you from generating cache both for "http://www.bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html" and for "http://bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html" (differing only in "www." part).</p><p>Here's what we need to do (some evident micro-tunings dropped for clarity):</p><ol><li>Hash the request URL.</li><li>Check if that cache item exists. If yes - go to step 4. If not - step 3.</li><li>Create cache item (cache file), set the file name to the hash of the request URL, and store the file in your cache directory (e.g. '/cache').</li><li>Read cache file modification timestamp.</li><li>Call HTTP_caching with that timestamp. If it returns true - do nothing. Else - continue to step 6.</li><li>Read cache file and send it to browser.</li></ol><p>To make this part of caching system wrapped in a single universally applicable function, you will need to define some "content-generating" handler function, which will be called when local cache file must be regenerated. "Content-generation" might be as simple as just reading some file from the disk; or you could wrap your index.php file in cache-checking block, so that page call and generation occur only if there is no readily available cached page.</p><div
class="igBar"><span
id="lphp-5"><a
href="#" onclick="javascript:showPlainTxt('php-5'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-5"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> caching<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$handler_function</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#FF9933; font-style:italic;">// first, check if we have existing cache</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#FF9933; font-style:italic;">// of currently requested resource in the 'cache' directory</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#0000FF;">$cachedfile</span> = <span
style="color:#FF0000;">'cache/'</span> . <a
href="http://www.php.net/md5"><span
style="color:#000066;">md5</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$_SERVER</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'REQUEST_URI'</span><span
style="color:#006600; font-weight:bold;">&#93;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><a
href="http://www.php.net/file_exists"><span
style="color:#000066;">file_exists</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span> &amp;&amp; <a
href="http://www.php.net/is_file"><span
style="color:#000066;">is_file</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#006600; font-weight:bold;">&#123;</span> <span
style="color:#FF9933; font-style:italic;">// cache exists. now check if it is up-to-date</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#0000FF;">$filetime</span> = <a
href="http://www.php.net/filemtime"><span
style="color:#000066;">filemtime</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#0000FF;">$modif</span> = <a
href="http://www.php.net/time"><span
style="color:#000066;">time</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> - <span
style="color:#0000FF;">$filetime</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#FF9933; font-style:italic;">// CACHE_PERIOD is a constant defined somewhere else,</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#FF9933; font-style:italic;">// e.g. define('CACHE_PERIOD',300)</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$modif</span> &lt;= <span
style="color:#006600; font-weight:bold;">&#40;</span>int<span
style="color:#006600; font-weight:bold;">&#41;</span>CACHE_PERIOD<span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <span
style="color:#616100;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> HTTP_caching<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$filetime</span><span
style="color:#006600; font-weight:bold;">&#41;</span> === <span
style="color:#000000; font-weight:bold;">false</span> <span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#123;</span> <span
style="color:#FF9933; font-style:italic;">// we need to send cached content to browser - no</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span
style="color:#FF9933; font-style:italic;">// if-modified-since or if-none-match request headers</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; <span
style="color:#0000FF;">$expires</span> = <a
href="http://www.php.net/gmdate"><span
style="color:#000066;">gmdate</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">'D, d M Y H:i:s'</span>, <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$filetime</span> + CACHE_PERIOD<span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">' GMT'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;&nbsp; send_cached_file<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span>, CACHE_PERIOD, <span
style="color:#0000FF;">$expires</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// if HTTP_caching returned 'true' - we are done with '304' header.</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// finally, ensure nothing else happens after sending contents...</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp; <a
href="http://www.php.net/exit"><span
style="color:#000066;">exit</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#FF9933; font-style:italic;">// if cache is old - just call $handler_function,</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#FF9933; font-style:italic;">// assuming that it returns all the content</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;&nbsp; &nbsp;<span
style="color:#0000FF;">$new_cache</span> = <span
style="color:#0000FF;">$handler_function</span><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// if instead of using $handler_function you wrap some code:</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// ob_start();</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// include &quot;page_generation.php&quot;;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// $new_cache = ob_get_contents();</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// ob_end_clean();</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$fp</span> = <a
href="http://www.php.net/fopen"><span
style="color:#000066;">fopen</span></a> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span>, <span
style="color:#FF0000;">"w"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/fwrite"><span
style="color:#000066;">fwrite</span></a> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$fp</span>, <span
style="color:#0000FF;">$new_cache</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/fclose"><span
style="color:#000066;">fclose</span></a> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$fp</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$filetime</span> = <a
href="http://www.php.net/filemtime"><span
style="color:#000066;">filemtime</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#0000FF;">$expires</span> = <a
href="http://www.php.net/gmdate"><span
style="color:#000066;">gmdate</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">'D, d M Y H:i:s'</span>, <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$filetime</span> + CACHE_PERIOD<span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">' GMT'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; send_cached_file<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span>, CACHE_PERIOD, <span
style="color:#0000FF;">$expires</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span
style="color:#006600; font-weight:bold;">&#125;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><p>Below is a function which sends cached files to the browser. Note, that it lacks content-type parameter (or content-type detection code).</p><div
class="igBar"><span
id="lphp-6"><a
href="#" onclick="javascript:showPlainTxt('php-6'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-6"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#000000; font-weight:bold;">function</span> send_cached_file<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span>, <span
style="color:#0000FF;">$cache</span>, <span
style="color:#0000FF;">$expires</span><span
style="color:#006600; font-weight:bold;">&#41;</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#123;</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// I omitted Content-Length and Content-Type for simplicity,</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// though it's better to send them</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"HTTP/1.1 200 OK"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Status: 200 OK"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Pragma: cache"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Cache-Control: max-age="</span>.<span
style="color:#0000FF;">$cache</span>.<span
style="color:#FF0000;">", min-fresh="</span>.<span
style="color:#0000FF;">$cache</span>.<span
style="color:#FF0000;">", no-transform"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// Content-Type needs adjustment on a per-content-item basis</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Content-Type: text/html"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Content-Length: "</span> . <a
href="http://www.php.net/filesize"><span
style="color:#000066;">filesize</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/header"><span
style="color:#000066;">header</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Expires: $expires"</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <a
href="http://www.php.net/readfile"><span
style="color:#000066;">readfile</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$cachedfile</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#006600; font-weight:bold;">&#125;</span></div></li></ol></div></div></div><p></p><p>(Note: function 'caching' was not tested; if you find any errors, or if it just doesn't work for you - let me know via the <a
href="http://bogdan.org.ua/contact">contact form</a>, I'll try to fix my error.)</p><p>For the explanation of what headers mean, refer to <a
href="http://bogdan.org.ua/2006/10/18/http-caching-request-and-response-headers.html" title="HTTP caching: request and response headers">my previous post</a>.</p><p>This is it for now. Coming next might be some useful information on differences in HTTP headers handling between PHP installed as a module and PHP as CGI.</p><p>As always, comments/suggestions are welcome.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;title=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" id="wpa2a_4"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Allow posting duplicate form-name entries with different values</title><link>http://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html</link> <comments>http://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html#comments</comments> <pubDate>Wed, 06 Sep 2006 09:04:41 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Bioinformatics]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Science]]></category> <category><![CDATA[http]]></category> <category><![CDATA[HTTP_Client]]></category> <category><![CDATA[HTTP_Request]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html</guid> <description><![CDATA[Sometimes, writing automatic HTML forms processors, you need to post several values with the same name of the form field, e.g.: collection_gene = str_chrom_name collection_gene = gene_stable_id This is against the RFC on form fields design and submitting, but this approach is used - for example, by Ensembl. I spent some time to figure out [...]]]></description> <content:encoded><![CDATA[<p>Sometimes, writing automatic HTML forms processors, you need to post several values with the same name of the form field, e.g.:<br
/> collection_gene = str_chrom_name<br
/> collection_gene = gene_stable_id</p><p>This is against the RFC on form fields design and submitting, but this approach is used - for example, by <a
href="http://www.ensembl.org/index.html" title="Ensembl genome automatic annotation database" target="_blank">Ensembl</a>. I spent some time to figure out how to make HTTP_Client and HTTP_Request submit multiple 'name-value' pairs instead of one (the latest defined, which overrides the previous). The solution is extremely simple:<br
/> <span
id="more-23"></span></p><div
class="igBar"><span
id="lphp-8"><a
href="#" onclick="javascript:showPlainTxt('php-8'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-8"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">/*</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;"> allow posting duplicate form-name</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;"> entries with different values</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#008000;">*/</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$params</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'useBrackets'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> = <span
style="color:#000000; font-weight:bold;">false</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$req</span> = <span
style="color:#000000; font-weight:bold;">&amp;new</span> HTTP_Client<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$params</span>, <span
style="color:#0000FF;">$headers</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>After setting 'useBrackets' parameter to 'false', HTTP_Client/HTTP_Request do post multiple lines as you would want it.</p><p>Hope this saves you some time.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;title=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" id="wpa2a_8"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Avoiding out of memory fatal error when using HTTP_Client or HTTP_Request</title><link>http://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html</link> <comments>http://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html#comments</comments> <pubDate>Wed, 06 Sep 2006 08:58:29 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Bioinformatics]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Science]]></category> <category><![CDATA[http]]></category> <category><![CDATA[HTTP_Client]]></category> <category><![CDATA[HTTP_Request]]></category> <category><![CDATA[memory]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html</guid> <description><![CDATA[If you fetch large amounts of data (e.g. over 2MB per request) using HTTP_Client (or HTTP_request), you may get "out of memory" fatal errors, especially if: memory_limit is set to default 8M, and you process multiple pages using single non-reset instance of HTTP_Client object. This problem can manifest itself by producing fatal error after a [...]]]></description> <content:encoded><![CDATA[<p>If you fetch large amounts of data (e.g. over 2MB per request) using HTTP_Client (or HTTP_request), you may get "out of memory" fatal errors, especially if:</p><ol><li>memory_limit is set to default 8M, and</li><li>you process multiple pages using single non-reset instance of HTTP_Client object.</li></ol><p>This problem can manifest itself by producing fatal error after a couple of cycles of successful page retrieval - but always, if run with the same parameters, after some constant or only slightly variable number of cycles.</p><p>In my case the problem was that HTTP_Request (a dependancy of HTTP_Client) was holding in memory all the previously fetched pages of the current session (the 'history' feature). To force HTTP_Request to hold only the most recent page, you need to 'disable' history after creating the HTTP_Client or HTTP_Request object instance:</p><div
class="igBar"><span
id="lphp-10"><a
href="#" onclick="javascript:showPlainTxt('php-10'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-10"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$req</span> = <span
style="color:#000000; font-weight:bold;">&amp;new</span> HTTP_Client<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$params</span>, <span
style="color:#0000FF;">$headers</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// disable history to save memory</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$req</span>-&gt;<span
style="color:#006600;">enableHistory</span><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#000000; font-weight:bold;">false</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>Hope this helps you.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;title=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" id="wpa2a_12"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using PEAR HTTP_Client or HTTP_Request with HTTP proxy</title><link>http://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html</link> <comments>http://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html#comments</comments> <pubDate>Wed, 06 Sep 2006 08:00:46 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[how-to]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[http]]></category> <category><![CDATA[HTTP_Client]]></category> <category><![CDATA[HTTP_Request]]></category> <category><![CDATA[PEAR]]></category> <category><![CDATA[proxy]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy.html</guid> <description><![CDATA[If you happen to write PHP script, which uses either HTTP_Client or its dependancy HTTP_Request from PEAR, and the script is supposed to work through the HTTP proxy - here are the sample settings you need: PLAIN TEXT PHP: $params&#91;'proxy_user'&#93; = 'proxy_username'; $params&#91;'proxy_pass'&#93; = 'proxy_password'; $params&#91;'proxy_host'&#93; = 'proxy_hostname_or_ip'; $params&#91;'proxy_port'&#93; = 8080; // 3128, ... &#160; [...]]]></description> <content:encoded><![CDATA[<p>If you happen to write PHP script, which uses either HTTP_Client or its dependancy HTTP_Request from PEAR, and the script is supposed to work through the HTTP proxy - here are the sample settings you need:</p><div
class="igBar"><span
id="lphp-12"><a
href="#" onclick="javascript:showPlainTxt('php-12'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-12"><div
class="php"><ol><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$params</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'proxy_user'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> = <span
style="color:#FF0000;">'proxy_username'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$params</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'proxy_pass'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> = <span
style="color:#FF0000;">'proxy_password'</span>;</div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$params</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'proxy_host'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> = <span
style="color:#FF0000;">'proxy_hostname_or_ip'</span>;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$params</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'proxy_port'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> = <span
style="color:#CC66CC;color:#800000;">8080</span>; <span
style="color:#FF9933; font-style:italic;">// 3128, ...</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#FF9933; font-style:italic;">// I assume $headers were set somewhere else</span></div></li><li
style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span
style="color:#0000FF;">$req</span> = <span
style="color:#000000; font-weight:bold;">&amp;new</span> HTTP_Client<span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$params</span>, <span
style="color:#0000FF;">$headers</span><span
style="color:#006600; font-weight:bold;">&#41;</span>;</div></li></ol></div></div></div><p></p><p>If your proxy does not need authorization - just drop the proxy_user and proxy_pass parameters.</p><p><a
class="a2a_button_google_plusone addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html"></a><a
class="a2a_button_facebook_like addtoany_special_service" data-href="http://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html"></a><a
class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;title=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" id="wpa2a_16"><img
src="http://bogdan.org.ua/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
