<?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 CavePHP &#187;</title> <atom:link href="http://bogdan.org.ua/tags/php/feed" rel="self" type="application/rss+xml" /><link>http://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Fri, 03 Feb 2012 22:51:18 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>fail2ban and Google translate: how to easily cut your WP blog traffic</title><link>http://bogdan.org.ua/2009/11/14/fail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html</link> <comments>http://bogdan.org.ua/2009/11/14/fail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html#comments</comments> <pubDate>Sat, 14 Nov 2009 00:11:08 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[*nix]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[WP PlugIns]]></category> <category><![CDATA[blog]]></category> <category><![CDATA[fail2ban]]></category> <category><![CDATA[fopen]]></category> <category><![CDATA[global trasnlator]]></category> <category><![CDATA[google translate]]></category> <category><![CDATA[maxretry]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[php-url-fopen]]></category> <category><![CDATA[regex]]></category> <category><![CDATA[rule]]></category> <category><![CDATA[traffic]]></category> <category><![CDATA[URL]]></category> <category><![CDATA[whitelist]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[WP]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=935</guid> <description><![CDATA[fail2ban has a php-url-fopen rule. WordPress has a Global Translator plugin, which &#8211; among others &#8211; uses Google Translate service. If someone uses Google Translate (e.g. using Global Translate&#8217;s mini-language-flags), and goes back to your blog &#8211; that someone might get banned by fail2ban (especially if you have set maxretry to 1), as the referrer [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://bogdan.org.ua/wp-content/uploads/2009/11/translate_logo.gif" alt="translate_logo" title="translate_logo" width="205" height="40" class="alignleft size-full wp-image-937" /><a
href="http://www.fail2ban.org/">fail2ban</a> has a php-url-fopen rule.</p><p><a
href="http://wordpress.org/">WordPress</a> has a <a
href="http://www.nothing2hide.net/wp-plugins/wordpress-global-translator-plugin/">Global Translator plugin</a>, which &#8211; among others &#8211; uses <a
href="http://translate.google.com/">Google Translate</a> service.</p><p>If someone uses Google Translate (e.g. using Global Translate&#8217;s mini-language-flags), and goes back to your blog &#8211; that someone might get banned by fail2ban (especially if you have set maxretry to 1), as the referrer will contain the php-URL-fopen attack signature. The bad thing is that you will not realize that until after you check one or several translations yourself, as a random site visitor experiencing the problem is highly unlikely to bother reporting this problem &#8211; especially when your blog&#8217;s Contact page is also inaccessible.</p><p>Clearly, Google Translate is not the only legitimate service which will trigger that rule.</p><p>Solution: The only solution I have found is to specify the whitelist regex for the php-URL-fopen rule.</p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2009/11/14/fail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Debugging PHP: Eclipse PDT + XDebug + XDebug helper</title><link>http://bogdan.org.ua/2009/06/08/debugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html</link> <comments>http://bogdan.org.ua/2009/06/08/debugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html#comments</comments> <pubDate>Sun, 07 Jun 2009 22:59:43 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[bug]]></category> <category><![CDATA[debugging]]></category> <category><![CDATA[development]]></category> <category><![CDATA[Eclipse]]></category> <category><![CDATA[FireFox]]></category> <category><![CDATA[PDT]]></category> <category><![CDATA[xdebug]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/?p=735</guid> <description><![CDATA[Stimulated by a bug in a complex and unfamiliar web PHP application with heaps of custom tweaks by other programmers, I decided to try a more professional approach to PHP programming and debugging than the standard var_dump() and family. As a result, I&#8217;m now using Eclipse PDT with Xdebug and Xdebug Helper (Firefox extension). Now [...]]]></description> <content:encoded><![CDATA[<p>Stimulated by a bug in a complex and unfamiliar web PHP application with heaps of custom tweaks by other programmers, I decided to try a more professional approach to PHP programming and debugging than the standard var_dump() and family.</p><p>As a result, I&#8217;m now using Eclipse <abbr
title="PHP Development Tools">PDT</abbr> with <a
href="http://xdebug.org/">Xdebug</a> and <a
href="https://addons.mozilla.org/uk/firefox/addon/3960">Xdebug Helper</a> (Firefox extension). Now I don&#8217;t understand how I used to debug my PHP programs before!</p><p>After proper configuration (I&#8217;m using local Apache, but it is also possible to debug remotely), my work flow is rather simple:</p><ul><li>use my web-app as usual, e.g. tweaking and testing here and there</li><li>if something server-side goes wrong: click the <a
href="https://addons.mozilla.org/uk/firefox/addon/3960">XDebug helper</a> icon in Firefox, and perform some server-request action (e.g. load a page)</li><li>debugging is started in Eclipse PDT, where I can step through the code, set breakpoints, and examine all variables</li><li>as soon as the problem is fixed &#8211; click the XDebug helper icon again to continue using the site normally (w/o invoking the debugger)</li></ul><p>It takes some time to get used to, but then it&#8217;s a breeze.</p><p>Some advice:</p><ul><li>don&#8217;t use apt-get/aptitude to install Eclipse; it will be much easier both in the short and long run to use some all-in-one package from the <a
href="http://www.eclipse.org/pdt/downloads/">Eclipse PDT site</a>; all you need to do &#8211; download, extract, run!</li><li>before actually starting to do anything, tweak the eclipse.ini file by increasing heap size from 40 MiB (default) to some larger value (I used 128MiB). If you don&#8217;t do this, then at some point your debugging will become painfully sloooow, and then you&#8217;ll start getting tons of &#8220;out of heap memory&#8221; errors, each one suggesting that you quit Eclipse immediately</li><li>install <a
href="http://xdebug.org/">XDebug</a> with apt-get/aptitude &#8211; worked perfectly, and there&#8217;s /etc/php5/conf.d/xdebug.ini not to mess with php.ini</li><li>do <strong>read</strong> <a
href="http://www.eclipse.org/pdt/documents/XDebugGuideForPDT2.0.pdf">XDebug guide for PDT 2.x</a> (I&#8217;m assuming you got the 2.x version); it should be the only document you will really need to configure everything</li></ul><p>I only wish Eclipse was faster &#8211; that is, written not in Java but e.g. C or C++.</p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2009/06/08/debugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html/feed</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Less than an hour of GoDaddy MySQL5 database downtime today</title><link>http://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html</link> <comments>http://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html#comments</comments> <pubDate>Wed, 26 Mar 2008 13:04:08 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Misc]]></category> <category><![CDATA[downtime]]></category> <category><![CDATA[godaddy]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html</guid> <description><![CDATA[Must have been some maintenance, as I didn&#8217;t notice any changes in PHP/MySQL versions since the 7th of March. Update: it seems as though since that short MySQL outage everything is faster at GoDaddy shared hosting. Did they upgrade database server(s)? I have no idea, but I like the change.]]></description> <content:encoded><![CDATA[<p>Must have been some maintenance, as I didn&#8217;t notice any changes in PHP/MySQL versions since the 7th of March.</p><p><ins
datetime="2008-04-06T07:01:54+00:00">Update:</ins> it seems as though since that short MySQL outage everything is faster at GoDaddy shared hosting. Did they upgrade database server(s)? I have no idea, but I like the change.</p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Convert MySQL database from one encoding/collation into another</title><link>http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</link> <comments>http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html#comments</comments> <pubDate>Fri, 08 Feb 2008 19:17:45 +0000</pubDate> <dc:creator>Bogdan</dc:creator> <category><![CDATA[Links]]></category> <category><![CDATA[Notepad]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[collation]]></category> <category><![CDATA[convert]]></category> <category><![CDATA[database]]></category> <category><![CDATA[encoding]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[latin1]]></category> <category><![CDATA[mysql]]></category> <category><![CDATA[script]]></category> <category><![CDATA[utf8]]></category> <guid
isPermaLink="false">http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</guid> <description><![CDATA[Most frequent use: convert database from latin1_swedish to utf8_general_ci. Original script found at: MySQL and UTF-8. Update: the original script had an error, it would generate queries likes this one (note the bold part): ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT NULL; This is clearly wrong syntax [...]]]></description> <content:encoded><![CDATA[<p>Most frequent use: <a
title="self-link" href="http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html">convert database from latin1_swedish to utf8_general_ci</a>.<br
/> Original script found at: <a
href="http://www.phpwact.org/php/i18n/utf-8/mysql">MySQL and UTF-8</a>.</p><p><ins
datetime="2008-02-22T21:50:28+00:00">Update:</ins> the original script <u>had an error</u>, it would generate queries likes this one (note the bold part):</p><blockquote><p>ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci <strong>NOT NULL  DEFAULT NULL</strong>;</p></blockquote><p>This is clearly wrong syntax (and logic). I fixed this by making comparison to NULL strict (with three equal signs instead of two):</p><blockquote><p> // Does the field default to null, a string, or nothing?<br
/> if ($row['Default'] === NULL)</p></blockquote><p><ins
datetime="2008-06-27T07:23:50+00:00">Update 2:</ins> based on comment by <em>banesto</em>, I modified the script; now it does not require specifying the from_collation, it's sufficient to specify to_collation (which will be used for all the fields and tables). The modified code is:</p><blockquote><p>if ($row['Collation'] == '' || $row['Collation'] == $convert_to)<br
/> continue;</p></blockquote><p><ins
datetime="2010-06-28T11:58:11+00:00">Update 3:</ins> the long-lasting, re-appearing NOT NULL DEFAULT NULL problem is finally fixed.</p><p><ins
datetime="2010-06-28T12:11:38+00:00">Update 4:</ins> incorporated Russ's fix to skip numeric fields (in order to leave autoincrement values intact).</p><p>Here's the script itself: (to copy-paste: first click the "Plain text" header)<br
/> <span
id="more-255"></span></p><div
class="igBar"><span
id="lphp-2"><a
href="#" onclick="javascript:showPlainTxt('php-2'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-2"><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;">&lt;?php</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;">// Original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql</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:#FF9933; font-style:italic;">// Improved/modified (v1.05) by Bogdan http://bogdan.org.ua/</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;">// Last updated: 2010-06-28</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;">// this script will output all queries needed to change all fields/tables to a different collation</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:#FF9933; font-style:italic;">// it is HIGHLY suggested you make a MySQL backup prior to running any of the generated queries</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;"><span
style="color:#FF9933; font-style:italic;">// this code is provided AS IS and without any warranty</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;"><span
style="color:#FF9933; font-style:italic;">// add text/plain header when used not as a CLI script; PHP CLI SAPI shouldn't output headers</span></div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><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/plain"</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;"><span
style="color:#FF9933; font-style:italic;">// precaution</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;"><a
href="http://www.php.net/die"><span
style="color:#000066;">die</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"Make a backup of your MySQL database, then remove this line from the code!"</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;"><a
href="http://www.php.net/set_time_limit"><span
style="color:#000066;">set_time_limit</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#CC66CC;color:#800000;">0</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;"><span
style="color:#FF9933; font-style:italic;">// collation you want to change to:</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;">$convert_to</span>&nbsp; &nbsp;= <span
style="color:#FF0000;">'utf8_general_ci'</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;">// character set of new collation:</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;">$character_set</span>= <span
style="color:#FF0000;">'utf8'</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;"><span
style="color:#FF9933; font-style:italic;">// DB login information - *modify before use*</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;">$username</span> = <span
style="color:#FF0000;">'user'</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;">$password</span> = <span
style="color:#FF0000;">'pass'</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;">$database</span> = <span
style="color:#FF0000;">'database_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:#0000FF;">$host</span>&nbsp; &nbsp; &nbsp;= <span
style="color:#FF0000;">'localhost'</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;"><span
style="color:#FF9933; font-style:italic;">//-- usually, there is nothing to modify below this line --//</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;"><span
style="color:#FF9933; font-style:italic;">// show TABLE alteration queries?</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;">$show_alter_table</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;"><span
style="color:#FF9933; font-style:italic;">// show FIELD alteration queries?</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;">$show_alter_field</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;</div></li><li
style="font-weight: bold;color:#26536A;"><div
style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a
href="http://www.php.net/mysql_connect"><span
style="color:#000066;">mysql_connect</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$host</span>, <span
style="color:#0000FF;">$username</span>, <span
style="color:#0000FF;">$password</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;"><a
href="http://www.php.net/mysql_select_db"><span
style="color:#000066;">mysql_select_db</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$database</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;"><span
style="color:#0000FF;">$rs_tables</span> = <a
href="http://www.php.net/mysql_query"><span
style="color:#000066;">mysql_query</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">" SHOW TABLES "</span><span
style="color:#006600; font-weight:bold;">&#41;</span> or <a
href="http://www.php.net/die"><span
style="color:#000066;">die</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><a
href="http://www.php.net/mysql_error"><span
style="color:#000066;">mysql_error</span></a><span
style="color:#006600; font-weight:bold;">&#40;</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;</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:#616100;">while</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row_tables</span> = <a
href="http://www.php.net/mysql_fetch_row"><span
style="color:#000066;">mysql_fetch_row</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$rs_tables</span><span
style="color:#006600; font-weight:bold;">&#41;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <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; <span
style="color:#0000FF;">$table</span> = <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row_tables</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#CC66CC;color:#800000;">0</span><span
style="color:#006600; font-weight:bold;">&#93;</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;</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;">// Alter table collation</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;">// ALTER TABLE `account` DEFAULT CHARACTER SET utf8</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;">if</span> <span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$show_alter_table</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; <a
href="http://www.php.net/echo"><span
style="color:#000066;">echo</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">"ALTER TABLE `$table` DEFAULT CHARACTER SET $character_set;<span
style="color:#000099; font-weight:bold;">\n</span>"</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;">$rs</span> = <a
href="http://www.php.net/mysql_query"><span
style="color:#000066;">mysql_query</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#FF0000;">" SHOW FULL FIELDS FROM `$table` "</span><span
style="color:#006600; font-weight:bold;">&#41;</span> or <a
href="http://www.php.net/die"><span
style="color:#000066;">die</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><a
href="http://www.php.net/mysql_error"><span
style="color:#000066;">mysql_error</span></a><span
style="color:#006600; font-weight:bold;">&#40;</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;</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;">while</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$row</span> = <a
href="http://www.php.net/mysql_fetch_assoc"><span
style="color:#000066;">mysql_fetch_assoc</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$rs</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <span
style="color:#006600; font-weight:bold;">&#41;</span> <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;</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;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Collation'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#FF0000;">''</span> || <span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Collation'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#0000FF;">$convert_to</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:#616100;">continue</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; &nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// Is the field allowed to be null?</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;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Null'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#FF0000;">'YES'</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;">$nullable</span> = <span
style="color:#FF0000;">' NULL '</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;">else</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;">$nullable</span> = <span
style="color:#FF0000;">' NOT NULL '</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; &nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// Does the field default to null, a string, or nothing?</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;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Default'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> === <span
style="color:#000000; font-weight:bold;">NULL</span> &amp;&amp; <span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Null'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> == <span
style="color:#FF0000;">'YES'</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;">$default</span> = <span
style="color:#FF0000;">" DEFAULT NULL "</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;">elseif</span> <span
style="color:#006600; font-weight:bold;">&#40;</span> <span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Default'</span><span
style="color:#006600; font-weight:bold;">&#93;</span> != <span
style="color:#FF0000;">''</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;">$default</span> = <span
style="color:#FF0000;">" DEFAULT '"</span>.<a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Default'</span><span
style="color:#006600; font-weight:bold;">&#93;</span><span
style="color:#006600; font-weight:bold;">&#41;</span>.<span
style="color:#FF0000;">"'"</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;">else</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;">$default</span> = <span
style="color:#FF0000;">''</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; &nbsp; &nbsp; <span
style="color:#FF9933; font-style:italic;">// sanity check and fix:</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;">$nullable</span> == <span
style="color:#FF0000;">' NOT NULL '</span> &amp;&amp; <span
style="color:#0000FF;">$default</span> == <span
style="color:#FF0000;">' DEFAULT NULL '</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <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; <span
style="color:#0000FF;">$default</span> = <span
style="color:#FF0000;">''</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/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">"/* Warning: wrong combination of 'default value' and 'NULL-flag' detected - and fixed! */<span
style="color:#000099; font-weight:bold;">\n</span>"</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/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">"/* Diagnostics: row[Null] = '$row[Null]', row[Default] = "</span> . <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Default'</span><span
style="color:#006600; font-weight:bold;">&#93;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">", MySQL version: "</span> . <a
href="http://www.php.net/mysql_get_server_info"><span
style="color:#000066;">mysql_get_server_info</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#006600; font-weight:bold;">&#41;</span> . <span
style="color:#FF0000;">" */<span
style="color:#000099; font-weight:bold;">\n</span>"</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;</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:#FF9933; font-style:italic;">// Don't alter INT columns: no collations, and altering them drops autoincrement values</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><a
href="http://www.php.net/strpos"><span
style="color:#000066;">strpos</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Type'</span><span
style="color:#006600; font-weight:bold;">&#93;</span>, <span
style="color:#FF0000;">'int'</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> <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; <span
style="color:#0000FF;">$show_alter_field</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; &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; &nbsp; &nbsp; <span
style="color:#616100;">else</span> <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; <span
style="color:#0000FF;">$show_alter_field</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;</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:#FF9933; font-style:italic;">// Alter field collation:</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:#FF9933; font-style:italic;">// ALTER TABLE `tab` CHANGE `field` `field` CHAR( 5 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL</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;">$show_alter_field</span><span
style="color:#006600; font-weight:bold;">&#41;</span> <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; <span
style="color:#0000FF;">$field</span> = <a
href="http://www.php.net/mysql_real_escape_string"><span
style="color:#000066;">mysql_real_escape_string</span></a><span
style="color:#006600; font-weight:bold;">&#40;</span><span
style="color:#0000FF;">$row</span><span
style="color:#006600; font-weight:bold;">&#91;</span><span
style="color:#FF0000;">'Field'</span><span
style="color:#006600; font-weight:bold;">&#93;</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/echo"><span
style="color:#000066;">echo</span></a> <span
style="color:#FF0000;">"ALTER TABLE `$table` CHANGE `$field` `$field` $row[Type] CHARACTER SET $character_set COLLATE $convert_to $nullable $default;<span
style="color:#000099; font-weight:bold;">\n</span>"</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;"><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;"><span
style="color:#000000; font-weight:bold;">?&gt;</span></div></li></ol></div></div></div><p></p> ]]></content:encoded> <wfw:commentRss>http://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html/feed</wfw:commentRss> <slash:comments>57</slash:comments> </item> <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-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> 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-7"><a
href="#" onclick="javascript:showPlainTxt('php-7'); return false;">PLAIN TEXT</a></span></div><div
class="syntax_hilite"><span
class="langName">PHP:</span><div
id="php-7"><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-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:#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> ]]></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>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-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;">$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> ]]></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>
