<?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 Cave &#187; http</title> <atom:link href="https://bogdan.org.ua/tags/http/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>HTTP caching: universal approach and sample code</title><link>https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html</link> <comments>https://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><![CDATA[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&#8217;s browser to cache content, and avoid unnecessary load on your servers. In this post I&#8217;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 [&#8230;]]]></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&#8217;s browser to cache content, and avoid unnecessary load on your servers. In this post I&#8217;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&#8217;s request &#8211; find out, if the browser is asking for content for the first time, or is looking for an updated version. If the browser&#8217;s content is fresh, just reply that it&#8217;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 &#8211; 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&#8217;s the headers part:</p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">function HTTP_caching($timestamp)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">{</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; /*</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* HTTP_caching(v0.3)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* checks headers for eTag or Modified request,</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* and replies with HTTP 304 Not Modified</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* if $timestamp is equal to the requested time/tag</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* returns TRUE if 304, FALSE otherwise</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* produces Last-Modified and ETag headers from $timestamp</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; */</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $latest = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $etag = md5($latest);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $not = false;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; if ( $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $latest )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;HTTP/1.1 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Status: 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $not = true;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; elseif ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; if ( $_SERVER['HTTP_IF_NONE_MATCH'] == $etag )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;HTTP/1.1 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Status: 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $not = true;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; if (!$not)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Last-Modified: &quot; . $latest );</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; header(&quot;ETag: &quot; . $etag);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; return $not;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">}</div></li></ol></div></div><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 &#8211; $timestamp &#8211; 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 &#8220;apache_request_headers&#8221;, 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&#8217;t work on all hosting providers &#8211; 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&#8217;m also sending two equal headers &#8211; 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&#8217;s move on to actual caching. The simplest and quite reliable method of identifying any object within your cache is md5(url) &#8211; 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&#8217;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 &#8220;xnameorg-down-largest-ddos-they-ever-had.html&#8221; part (or &#8220;2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221;, if the filename part of the path might be non-unique). Evidently, this will save you from generating cache both for &#8220;http://www.bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221; and for &#8220;http://bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221; (differing only in &#8220;www.&#8221; part).</p><p>Here&#8217;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 &#8211; go to step 4. If not &#8211; 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. &#8216;/cache&#8217;).</li><li>Read cache file modification timestamp.</li><li>Call HTTP_caching with that timestamp. If it returns true &#8211; do nothing. Else &#8211; 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 &#8220;content-generating&#8221; handler function, which will be called when local cache file must be regenerated. &#8220;Content-generation&#8221; 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><p>function caching($handler_function)<br
/> {<br
/> // first, check if we have existing cache<br
/> // of currently requested resource in the &#8216;cache&#8217; directory<br
/> $cachedfile = &#8216;cache/&#8217; . md5($_SERVER['REQUEST_URI']);<br
/> if (file_exists($cachedfile) &#038;&#038; is_file($cachedfile))<br
/> { // cache exists. now check if it is up-to-date<br
/> $filetime = filemtime($cachedfile);<br
/> $modif = time() &#8211; $filetime;<br
/> // CACHE_PERIOD is a constant defined somewhere else,<br
/> // e.g. define(&#8216;CACHE_PERIOD&#8217;,300)<br
/> if ($modif <= (int)CACHE_PERIOD)
{
if ( HTTP_caching($filetime) === false )
{ // we need to send cached content to browser - no
// if-modified-since or if-none-match request headers
$expires = gmdate('D, d M Y H:i:s', ($filetime + CACHE_PERIOD) ) . ' GMT';
send_cached_file($cachedfile, CACHE_PERIOD, $expires)
}
// if HTTP_caching returned 'true' - we are done with '304' header.
// finally, ensure nothing else happens after sending contents...
exit();
}
// if cache is old - just call $handler_function,
// assuming that it returns all the content
$new_cache = $handler_function();
// if instead of using $handler_function you wrap some code:
// ob_start();
// include "page_generation.php";
// $new_cache = ob_get_contents();
// ob_end_clean();
$fp = fopen ($cachedfile, "w");
fwrite ($fp, $new_cache);
fclose ($fp);
$filetime = filemtime($cachedfile);
$expires = gmdate('D, d M Y H:i:s', ($filetime + CACHE_PERIOD) ) . ' GMT';
send_cached_file($cachedfile, CACHE_PERIOD, $expires);
}
}
[/php]
Below is a function which sends cached files to the browser. Note, that it lacks content-type parameter (or content-type detection code).
[php]
function send_cached_file($cachedfile, $cache, $expires)
{
// I omitted Content-Length and Content-Type for simplicity,
// though it's better to send them
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
header("Pragma: cache");
header("Cache-Control: max-age=".$cache.", min-fresh=".$cache.", no-transform");
// Content-Type needs adjustment on a per-content-item basis
header("Content-Type: text/html");
header("Content-Length: " . filesize($cachedfile));
header("Expires: $expires");
readfile($cachedfile);
}
[/php]
(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&#8217;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_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&#038;title=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" data-a2a-url="https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html" data-a2a-title="HTTP caching: universal approach and sample code"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html</link> <comments>https://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><![CDATA[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 &#8211; for example, by Ensembl. I spent some time to figure out [&#8230;]]]></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 &#8211; 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 &#8216;name-value&#8217; pairs instead of one (the latest defined, which overrides the previous). The solution is extremely simple:<br
/> <span
id="more-23"></span></p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">/*</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;allow posting duplicate form-name</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;entries with different values</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">*/</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['useBrackets'] = false;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$req = &amp;new HTTP_Client($params, $headers);</div></li></ol></div></div><p>After setting &#8216;useBrackets&#8217; parameter to &#8216;false&#8217;, 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_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;linkname=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;linkname=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;linkname=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;linkname=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&amp;linkname=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fallow-posting-duplicate-form-name-entries-with-different-values.html&#038;title=Allow%20posting%20duplicate%20form-name%20entries%20with%20different%20values" data-a2a-url="https://bogdan.org.ua/2006/09/06/allow-posting-duplicate-form-name-entries-with-different-values.html" data-a2a-title="Allow posting duplicate form-name entries with different values"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html</link> <comments>https://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><![CDATA[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 &#8220;out of memory&#8221; 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 [&#8230;]]]></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 &#8220;out of memory&#8221; 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 &#8211; 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 &#8216;history&#8217; feature). To force HTTP_Request to hold only the most recent page, you need to &#8216;disable&#8217; history after creating the HTTP_Client or HTTP_Request object instance:</p><div
id="ig-sh-3" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$req = &amp;new HTTP_Client($params, $headers);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">// disable history to save memory</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$req-&gt;enableHistory(false);</div></li></ol></div></div><p>Hope this helps you.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;linkname=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;linkname=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;linkname=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;linkname=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&amp;linkname=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Favoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html&#038;title=Avoiding%20out%20of%20memory%20fatal%20error%20when%20using%20HTTP_Client%20or%20HTTP_Request" data-a2a-url="https://bogdan.org.ua/2006/09/06/avoiding-out-of-memory-fatal-error-when-using-http_client-or-http_request.html" data-a2a-title="Avoiding out of memory fatal error when using HTTP_Client or HTTP_Request"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html</link> <comments>https://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><![CDATA[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 &#8211; here are the sample settings you need: $params['proxy_user'] = 'proxy_username'; $params['proxy_pass'] = 'proxy_password'; $params['proxy_host'] = 'proxy_hostname_or_ip'; $params['proxy_port'] = 8080; // 3128, ... &#160; // I assume [&#8230;]]]></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 &#8211; here are the sample settings you need:</p><div
id="ig-sh-4" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_user'] = 'proxy_username';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_pass'] = 'proxy_password';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_host'] = 'proxy_hostname_or_ip';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_port'] = 8080; // 3128, ...</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">// I assume $headers were set somewhere else</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$req = &amp;new HTTP_Client($params, $headers);</div></li></ol></div></div><p>If your proxy does not need authorization &#8211; just drop the proxy_user and proxy_pass parameters.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&#038;title=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" data-a2a-url="https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html" data-a2a-title="Using PEAR HTTP_Client or HTTP_Request with HTTP proxy"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>