<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Autarchy of the Private Cave &#187; PHP</title> <atom:link href="https://bogdan.org.ua/tags/php/feed" rel="self" type="application/rss+xml" /><link>https://bogdan.org.ua</link> <description>Tiny bits of bioinformatics, [web-]programming etc</description> <lastBuildDate>Wed, 28 Dec 2022 16:09:04 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>https://wordpress.org/?v=3.8.27</generator> <item><title>fail2ban and Google translate: how to easily cut your WP blog traffic</title><link>https://bogdan.org.ua/2009/11/14/fail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html</link> <comments>https://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><![CDATA[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 [&#8230;]]]></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/" class="broken_link" rel="nofollow">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><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&amp;linkname=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&amp;linkname=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&amp;linkname=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&amp;linkname=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&amp;linkname=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2009%2F11%2F14%2Ffail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html&#038;title=fail2ban%20and%20Google%20translate%3A%20how%20to%20easily%20cut%20your%20WP%20blog%20traffic" data-a2a-url="https://bogdan.org.ua/2009/11/14/fail2ban-and-google-translate-how-to-easily-cut-your-wp-blog-traffic.html" data-a2a-title="fail2ban and Google translate: how to easily cut your WP blog traffic"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2009/06/08/debugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html</link> <comments>https://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><![CDATA[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 [&#8230;]]]></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" class="broken_link" rel="nofollow">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" class="broken_link" rel="nofollow">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><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&amp;linkname=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&amp;linkname=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&amp;linkname=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&amp;linkname=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&amp;linkname=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2009%2F06%2F08%2Fdebugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html&#038;title=Debugging%20PHP%3A%20Eclipse%20PDT%20%2B%20XDebug%20%2B%20XDebug%20helper" data-a2a-url="https://bogdan.org.ua/2009/06/08/debugging-php-like-a-pro-eclipse-pdt-xdebug-helper.html" data-a2a-title="Debugging PHP: Eclipse PDT + XDebug + XDebug helper"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html</link> <comments>https://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><![CDATA[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><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&amp;linkname=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&amp;linkname=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&amp;linkname=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&amp;linkname=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&amp;linkname=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2008%2F03%2F26%2Fless-than-an-hour-of-godaddy-mysql5-database-downtime-today.html&#038;title=Less%20than%20an%20hour%20of%20GoDaddy%20MySQL5%20database%20downtime%20today" data-a2a-url="https://bogdan.org.ua/2008/03/26/less-than-an-hour-of-godaddy-mysql5-database-downtime-today.html" data-a2a-title="Less than an hour of GoDaddy MySQL5 database downtime today"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://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>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html</link> <comments>https://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><![CDATA[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 [&#8230;]]]></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" class="broken_link" rel="nofollow">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&#8217;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'] == &#8221; || $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&#8217;s fix to skip numeric fields (in order to leave autoincrement values intact).</p><p>Here&#8217;s the script itself: (to copy-paste: first click the &#8220;Plain text&#8221; header)<br
/> <span
id="more-255"></span></p><div
id="ig-sh-1" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Original script (v1.0) by/from: http://www.phpwact.org/php/i18n/utf-8/mysql</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Improved/modified (v1.05) by Bogdan http://bogdan.org.ua/</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// Last updated: 2010-06-28</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; 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-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; 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: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// this code is provided AS IS and without any warranty</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; 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: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">header</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Content-Type: text/plain&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// precaution</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;Make a backup of your MySQL database, then remove this line from the code!&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">set_time_limit</span><span
style="color: #009900;">&#40;</span><span
style="color: #cc66cc;">0</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// collation you want to change to:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$convert_to</span> &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8_general_ci'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// character set of new collation:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$character_set</span><span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'utf8'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// DB login information - *modify before use*</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$username</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'user'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$password</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'pass'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$database</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'database_name'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$host</span> &nbsp; &nbsp; <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">'localhost'</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">//-- usually, there is nothing to modify below this line --//</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// show TABLE alteration queries?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$show_alter_table</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #666666; font-style: italic;">// show FIELD alteration queries?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">true</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">mysql_connect</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$host</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$username</span><span
style="color: #339933;">,</span> <span
style="color: #000088;">$password</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #990000;">mysql_select_db</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$database</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000088;">$rs_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW TABLES &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_row</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs_tables</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #000088;">$table</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row_tables</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;0&amp;#93;);</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter table collation</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// ALTER TABLE `account` DEFAULT CHARACTER SET utf8</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_table</span><span
style="color: #009900;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` DEFAULT CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #000088;">$rs</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_query</span><span
style="color: #009900;">&#40;</span><span
style="color: #0000ff;">&quot; SHOW FULL FIELDS FROM `<span
style="color: #006699; font-weight: bold;">$table</span>` &quot;</span><span
style="color: #009900;">&#41;</span> or <span
style="color: #990000;">die</span><span
style="color: #009900;">&#40;</span><span
style="color: #990000;">mysql_error</span><span
style="color: #009900;">&#40;</span><span
style="color: #009900;">&#41;</span><span
style="color: #009900;">&#41;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #b1b100;">while</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_fetch_assoc</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$rs</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Collation'&amp;#93; == '' || $row&amp;#91;'Collation'&amp;#93; == $convert_to )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">continue</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Is the field allowed to be null?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Null'&amp;#93; == 'YES' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NULL '</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">' NOT NULL '</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Does the field default to null, a string, or nothing?</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; === NULL &amp;&amp; $row&amp;#91;'Null'&amp;#93; == 'YES' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT NULL &quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">elseif</span> <span
style="color: #009900;">&#40;</span> <span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93; != '' )</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">&quot; DEFAULT '&quot;</span><span
style="color: #339933;">.</span><span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;).&quot;'&quot;;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">else</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// sanity check and fix:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$nullable</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' NOT NULL '</span> <span
style="color: #339933;">&amp;&amp;</span> <span
style="color: #000088;">$default</span> <span
style="color: #339933;">==</span> <span
style="color: #0000ff;">' DEFAULT NULL '</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$default</span> <span
style="color: #339933;">=</span> <span
style="color: #0000ff;">''</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Warning: wrong combination of 'default value' and 'NULL-flag' detected - and fixed! */<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;/* Diagnostics: row&amp;#91;Null&amp;#93; = '<span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Null&amp;#93;', row&amp;#91;Default&amp;#93; = &quot;</span> <span
style="color: #339933;">.</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Default'&amp;#93;) . &quot;, MySQL version: &quot; . mysql_get_server_info() . &quot; */\n&quot;;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Don't alter INT columns: no collations, and altering them drops autoincrement values</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #990000;">strpos</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Type'&amp;#93;, 'int') !== false) {</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">False</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">else</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$show_alter_field</span> <span
style="color: #339933;">=</span> <span
style="color: #009900; font-weight: bold;">True</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; font-style: italic;">// Alter field collation:</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #666666; 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-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #b1b100;">if</span> <span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$show_alter_field</span><span
style="color: #009900;">&#41;</span> <span
style="color: #009900;">&#123;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #000088;">$field</span> <span
style="color: #339933;">=</span> <span
style="color: #990000;">mysql_real_escape_string</span><span
style="color: #009900;">&#40;</span><span
style="color: #000088;">$row</span><span
style="color: #339933;">&amp;</span><span
style="color: #666666; font-style: italic;">#91;'Field'&amp;#93;);</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span
style="color: #b1b100;">echo</span> <span
style="color: #0000ff;">&quot;ALTER TABLE `<span
style="color: #006699; font-weight: bold;">$table</span>` CHANGE `<span
style="color: #006699; font-weight: bold;">$field</span>` `<span
style="color: #006699; font-weight: bold;">$field</span>` <span
style="color: #006699; font-weight: bold;">$row</span>&amp;#91;Type&amp;#93; CHARACTER SET <span
style="color: #006699; font-weight: bold;">$character_set</span> COLLATE <span
style="color: #006699; font-weight: bold;">$convert_to</span> <span
style="color: #006699; font-weight: bold;">$nullable</span> <span
style="color: #006699; font-weight: bold;">$default</span>;<span
style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span
style="color: #339933;">;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #009900;">&#125;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #000000; font-weight: bold;">?&gt;</span></div></li></ol></div></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&amp;linkname=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2008%2F02%2F08%2Fconvert-mysql-database-from-one-encodingcollation-into-another.html&#038;title=Convert%20MySQL%20database%20from%20one%20encoding%2Fcollation%20into%20another" data-a2a-url="https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html" data-a2a-title="Convert MySQL database from one encoding/collation into another"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2008/02/08/convert-mysql-database-from-one-encodingcollation-into-another.html/feed</wfw:commentRss> <slash:comments>60</slash:comments> </item> <item><title>HTTP caching: universal approach and sample code</title><link>https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html</link> <comments>https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html#comments</comments> <pubDate>Sat, 09 Dec 2006 09:16:53 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Web]]></category> <category><![CDATA[caching]]></category> <category><![CDATA[how-to]]></category> <category><![CDATA[http]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html</guid> <description><![CDATA[As described in my previous post, there are some rather simple mechanisms to enable visitor&#8217;s browser to cache content, and avoid unnecessary load on your servers. In this post I&#8217;ll take a look at some parts of the practical implementation of the caching mechanism on the server, using PHP. I think of caching as a [&#8230;]]]></description> <content:encoded><![CDATA[<p>As described in my <a
href="http://bogdan.org.ua/2006/10/18/http-caching-request-and-response-headers.html" title="HTTP caching: request and response headers">previous post</a>, there are some rather simple mechanisms to enable visitor&#8217;s browser to cache content, and avoid unnecessary load on your servers. In this post I&#8217;ll take a look at some parts of the practical implementation of the caching mechanism on the server, using PHP.<br
/> <span
id="more-50"></span><br
/> I think of caching as a set of relatively independent procedures:</p><ol><li>Process browser&#8217;s request &#8211; find out, if the browser is asking for content for the first time, or is looking for an updated version. If the browser&#8217;s content is fresh, just reply that it&#8217;s fresh. Otherwise, proceed to step 2. Also proceed to step 2, if this is a first-time request.</li><li>If this is a first-time request, check if we have a fresh locally cached content, and send it in response, to bypass <em>de novo</em> contet generation. If local cache is stale, proceed to step 3.</li><li>Generate new (fresh) content cache item, and send it to browser, together with cache-enabling headers.</li></ol><p>I decided to put all the header-related processing into one function, and all the cache-creation and sending &#8211; into another. If you prefer, you can make an object with two or more methods, but I see no need in doing so.</p><p>Here&#8217;s the headers part:</p><div
id="ig-sh-2" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">function HTTP_caching($timestamp)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">{</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; /*</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* HTTP_caching(v0.3)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* checks headers for eTag or Modified request,</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* and replies with HTTP 304 Not Modified</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* if $timestamp is equal to the requested time/tag</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* returns TRUE if 304, FALSE otherwise</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp;* produces Last-Modified and ETag headers from $timestamp</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; */</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $latest = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $etag = md5($latest);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; $not = false;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; if ( $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $latest )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;HTTP/1.1 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Status: 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $not = true;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; elseif ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; if ( $_SERVER['HTTP_IF_NONE_MATCH'] == $etag )</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;HTTP/1.1 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Status: 304 Not Modified&quot;);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $not = true;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; if (!$not)</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; {</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; header(&quot;Last-Modified: &quot; . $latest );</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; header(&quot;ETag: &quot; . $etag);</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; }</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; return $not;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">}</div></li></ol></div></div><p>This function checks two caching-related request headers: If-None-Match and If-Modified-Since. Use of two headers is redundant, but this was done with an idea to provide different caching methods for different content types in the future. Timestamp-based approach should be sufficient in most cases, but having ETag as well does not hurt.</p><p>The parameter this function takes &#8211; $timestamp &#8211; is the date of the locally cached content item. We should check if the cached item exists before calling HTTP_caching; and if cached item does not exist, we should create it and then call HTTP_caching with the timestamp of the newly generated cached content item.</p><p>Function is utterly simple, but needs some explaining comments:</p><ul><li>There is a convenient function &#8220;apache_request_headers&#8221;, but as its name implies, it works only under apache. Thus I had to use global PHP variables for fetching specific request headers. However, this approach also doesn&#8217;t work on all hosting providers &#8211; you just do not get those headers, though they are sent by the browser. But you still benefit from local caching, and the only drawback of being unable to process request headers is that you waste traffic sending data which could have been unsent.</li><li>I&#8217;m also sending two equal headers &#8211; standard HTTP/1.1, and outdated Status header. This is redundant (sending Status might be removed), but depending on how your PHP is installed (as apache-httpd module or CGI) you may want to leave both in place, or experiment and leave the one which actually works.</li></ul><p>Now, let&#8217;s move on to actual caching. The simplest and quite reliable method of identifying any object within your cache is md5(url) &#8211; that is, the hash of the request URL. Note, that you might want to hash not the complete URL (starting with http://), but only the part after the TLD&#8217;s slash, e.g. for complete URL http://bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html you would hash only the &#8220;xnameorg-down-largest-ddos-they-ever-had.html&#8221; part (or &#8220;2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221;, if the filename part of the path might be non-unique). Evidently, this will save you from generating cache both for &#8220;http://www.bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221; and for &#8220;http://bogdan.org.ua/2006/10/27/xnameorg-down-largest-ddos-they-ever-had.html&#8221; (differing only in &#8220;www.&#8221; part).</p><p>Here&#8217;s what we need to do (some evident micro-tunings dropped for clarity):</p><ol><li>Hash the request URL.</li><li>Check if that cache item exists. If yes &#8211; go to step 4. If not &#8211; step 3.</li><li>Create cache item (cache file), set the file name to the hash of the request URL, and store the file in your cache directory (e.g. &#8216;/cache&#8217;).</li><li>Read cache file modification timestamp.</li><li>Call HTTP_caching with that timestamp. If it returns true &#8211; do nothing. Else &#8211; continue to step 6.</li><li>Read cache file and send it to browser.</li></ol><p>To make this part of caching system wrapped in a single universally applicable function, you will need to define some &#8220;content-generating&#8221; handler function, which will be called when local cache file must be regenerated. &#8220;Content-generation&#8221; might be as simple as just reading some file from the disk; or you could wrap your index.php file in cache-checking block, so that page call and generation occur only if there is no readily available cached page.</p><p>function caching($handler_function)<br
/> {<br
/> // first, check if we have existing cache<br
/> // of currently requested resource in the &#8216;cache&#8217; directory<br
/> $cachedfile = &#8216;cache/&#8217; . md5($_SERVER['REQUEST_URI']);<br
/> if (file_exists($cachedfile) &#038;&#038; is_file($cachedfile))<br
/> { // cache exists. now check if it is up-to-date<br
/> $filetime = filemtime($cachedfile);<br
/> $modif = time() &#8211; $filetime;<br
/> // CACHE_PERIOD is a constant defined somewhere else,<br
/> // e.g. define(&#8216;CACHE_PERIOD&#8217;,300)<br
/> if ($modif <= (int)CACHE_PERIOD)
{
if ( HTTP_caching($filetime) === false )
{ // we need to send cached content to browser - no
// if-modified-since or if-none-match request headers
$expires = gmdate('D, d M Y H:i:s', ($filetime + CACHE_PERIOD) ) . ' GMT';
send_cached_file($cachedfile, CACHE_PERIOD, $expires)
}
// if HTTP_caching returned 'true' - we are done with '304' header.
// finally, ensure nothing else happens after sending contents...
exit();
}
// if cache is old - just call $handler_function,
// assuming that it returns all the content
$new_cache = $handler_function();
// if instead of using $handler_function you wrap some code:
// ob_start();
// include "page_generation.php";
// $new_cache = ob_get_contents();
// ob_end_clean();
$fp = fopen ($cachedfile, "w");
fwrite ($fp, $new_cache);
fclose ($fp);
$filetime = filemtime($cachedfile);
$expires = gmdate('D, d M Y H:i:s', ($filetime + CACHE_PERIOD) ) . ' GMT';
send_cached_file($cachedfile, CACHE_PERIOD, $expires);
}
}
[/php]
Below is a function which sends cached files to the browser. Note, that it lacks content-type parameter (or content-type detection code).
[php]
function send_cached_file($cachedfile, $cache, $expires)
{
// I omitted Content-Length and Content-Type for simplicity,
// though it's better to send them
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
header("Pragma: cache");
header("Cache-Control: max-age=".$cache.", min-fresh=".$cache.", no-transform");
// Content-Type needs adjustment on a per-content-item basis
header("Content-Type: text/html");
header("Content-Length: " . filesize($cachedfile));
header("Expires: $expires");
readfile($cachedfile);
}
[/php]
(Note: function 'caching' was not tested; if you find any errors, or if it just doesn't work for you - let me know via the <a
href="http://bogdan.org.ua/contact">contact form</a>, I&#8217;ll try to fix my error.)</p><p>For the explanation of what headers mean, refer to <a
href="http://bogdan.org.ua/2006/10/18/http-caching-request-and-response-headers.html" title="HTTP caching: request and response headers">my previous post</a>.</p><p>This is it for now. Coming next might be some useful information on differences in HTTP headers handling between PHP installed as a module and PHP as CGI.</p><p>As always, comments/suggestions are welcome.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&amp;linkname=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F12%2F09%2Fhttp-caching-universal-approach-and-sample-code.html&#038;title=HTTP%20caching%3A%20universal%20approach%20and%20sample%20code" data-a2a-url="https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html" data-a2a-title="HTTP caching: universal approach and sample code"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2006/12/09/http-caching-universal-approach-and-sample-code.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Using PEAR HTTP_Client or HTTP_Request with HTTP proxy</title><link>https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html</link> <comments>https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html#comments</comments> <pubDate>Wed, 06 Sep 2006 08:00:46 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[how-to]]></category> <category><![CDATA[PHP]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[http]]></category> <category><![CDATA[HTTP_Client]]></category> <category><![CDATA[HTTP_Request]]></category> <category><![CDATA[PEAR]]></category> <category><![CDATA[proxy]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy.html</guid> <description><![CDATA[If you happen to write PHP script, which uses either HTTP_Client or its dependancy HTTP_Request from PEAR, and the script is supposed to work through the HTTP proxy &#8211; here are the sample settings you need: $params['proxy_user'] = 'proxy_username'; $params['proxy_pass'] = 'proxy_password'; $params['proxy_host'] = 'proxy_hostname_or_ip'; $params['proxy_port'] = 8080; // 3128, ... &#160; // I assume [&#8230;]]]></description> <content:encoded><![CDATA[<p>If you happen to write PHP script, which uses either HTTP_Client or its dependancy HTTP_Request from PEAR, and the script is supposed to work through the HTTP proxy &#8211; here are the sample settings you need:</p><div
id="ig-sh-3" class="syntax_hilite"><div
class="code"><ol
class="php" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_user'] = 'proxy_username';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_pass'] = 'proxy_password';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_host'] = 'proxy_hostname_or_ip';</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$params['proxy_port'] = 8080; // 3128, ...</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">// I assume $headers were set somewhere else</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">$req = &amp;new HTTP_Client($params, $headers);</div></li></ol></div></div><p>If your proxy does not need authorization &#8211; just drop the proxy_user and proxy_pass parameters.</p><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&amp;linkname=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F09%2F06%2Fusing-pear-http_client-or-http_request-with-http-proxy-example.html&#038;title=Using%20PEAR%20HTTP_Client%20or%20HTTP_Request%20with%20HTTP%20proxy" data-a2a-url="https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html" data-a2a-title="Using PEAR HTTP_Client or HTTP_Request with HTTP proxy"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2006/09/06/using-pear-http_client-or-http_request-with-http-proxy-example.html/feed</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Simple substring counting script in Python</title><link>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html</link> <comments>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html#comments</comments> <pubDate>Wed, 21 Jun 2006 17:41:27 +0000</pubDate> <dc:creator><![CDATA[Bogdan]]></dc:creator> <category><![CDATA[Programming]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[perl]]></category> <category><![CDATA[PHP]]></category> <guid
isPermaLink="false">http://www.bogdan.org.ua/blog/2006/06/21/simple-substring-counting-script-in-python.html</guid> <description><![CDATA[Approximately a month ago I endeavoured to use Python as my main shell-scripting language. At that moment, I was already aware of multiple benefits you get when you use Python for scripting: source-level cross-platform scripting: your script will run anywhere, where Python compiles; expanding this statement &#8211; your script will run anywhere, where there is [&#8230;]]]></description> <content:encoded><![CDATA[<p>Approximately a month ago I endeavoured to use Python as my main shell-scripting language. At that moment, I was already aware of multiple benefits you get when you use Python for scripting:</p><ul><li>source-level cross-platform scripting: your script will run anywhere, where Python compiles; expanding this statement &#8211; your script will run anywhere, where there is a C compiler (needed to build Python itself)</li><li>high-level language: you can iterate all the lines in a text file with as little as one &#8216;for&#8217;-statement, for example (see the actual example below)</li><li>simple/minimalist syntax: no curly braces around blocks of statements, no semicolons after each and every line of code, etc. Python at a glance looks much more understandable, than, for example, Perl.</li><li>the power of C in a language-interpreting system</li><li>it is interpreted! This gives easyness of debugging: modify, execute, see the trouble &#8211; with no compile/link stages</li><li>and, despite being interpreted, it is fast!</li></ul><p>For the comparison (in speed, memory use, program size) with other computer programming languages, please see the &#8220;Computer Language Shootout Benchmarks&#8221;. Here I provide the link only to the <a
href="http://benchmarksgame.alioth.debian.org/u64/benchmark.php?test=all&amp;lang=python3&amp;lang2=perl&amp;data=u64" target="_blank" title="Python vs Perl comparison" class="broken_link" rel="nofollow">comparison of Python with Perl</a> and <a
href="http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&amp;lang=python3&amp;lang2=php&amp;data=u32" title="Python vs PHP" target="_blank" class="broken_link" rel="nofollow">comparison of Python with PHP</a> (which can also be used as shell-scripting language, albeit after some tinkering with settings and stuff)</p><p>Below is an example of the 2-minute script in Python, which counts the number of occurrences of some string in a file.</p><div
id="ig-sh-4" class="syntax_hilite"><div
class="code"><ol
class="python" style="font-family:monospace;"><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #483d8b;">&quot;&quot;&quot;Read FILE and count number of occurences of SUBSTR.&quot;&quot;&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">version <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0.01</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">import</span> <span
style="color: #dc143c;">sys</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">def</span> main<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">from</span> <span
style="color: #dc143c;">optparse</span> <span
style="color: #ff7700;font-weight:bold;">import</span> OptionParser</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts <span
style="color: #66cc66;">=</span> OptionParser<span
style="color: black;">&#40;</span>usage<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;%prog [options] FILE SUBSTR&quot;</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; version<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;%prog &quot;</span> + <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>version<span
style="color: black;">&#41;</span><span
style="color: #66cc66;">,</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; description<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;Read FILE and count number of occurences of SUBSTR.&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">set_defaults</span><span
style="color: black;">&#40;</span>verbose<span
style="color: #66cc66;">=</span><span
style="color: #008000;">False</span><span
style="color: #66cc66;">,</span>flush<span
style="color: #66cc66;">=</span><span
style="color: #008000;">False</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">add_option</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;-v&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">&quot;--verbose&quot;</span><span
style="color: #66cc66;">,</span> action<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;store_true&quot;</span><span
style="color: #66cc66;">,</span> dest<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;verbose&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">help</span><span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;Print every line containing substr [default: %default]&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; opts.<span
style="color: black;">add_option</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;-f&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">&quot;--flush&quot;</span><span
style="color: #66cc66;">,</span> action<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;store_true&quot;</span><span
style="color: #66cc66;">,</span> dest<span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;flush&quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">help</span><span
style="color: #66cc66;">=</span><span
style="color: #483d8b;">&quot;When verbose, flush every line [default: %default]&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: black;">&#40;</span>options<span
style="color: #66cc66;">,</span> args<span
style="color: black;">&#41;</span> <span
style="color: #66cc66;">=</span> opts.<span
style="color: black;">parse_args</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> <span
style="color: #008000;">len</span><span
style="color: black;">&#40;</span>args<span
style="color: black;">&#41;</span> <span
style="color: #66cc66;">!=</span> <span
style="color: #ff4500;">2</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Two arguments required for correct processing&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; opts.<span
style="color: black;">print_help</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #dc143c;">sys</span>.<span
style="color: black;">exit</span><span
style="color: black;">&#40;</span><span
style="color: #ff4500;">2</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; infile <span
style="color: #66cc66;">=</span> args<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">0</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; substr <span
style="color: #66cc66;">=</span> args<span
style="color: black;">&#91;</span><span
style="color: #ff4500;">1</span><span
style="color: black;">&#93;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; lines_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; substr_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; lines_substr_count <span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">0</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; msg <span
style="color: #66cc66;">=</span> <span
style="color: #483d8b;">&quot;&quot;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; f <span
style="color: #66cc66;">=</span> <span
style="color: #008000;">open</span><span
style="color: black;">&#40;</span>infile<span
style="color: #66cc66;">,</span> <span
style="color: #483d8b;">'r'</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">for</span> line <span
style="color: #ff7700;font-weight:bold;">in</span> f:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; lines_count +<span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">1</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; found <span
style="color: #66cc66;">=</span> line.<span
style="color: black;">count</span><span
style="color: black;">&#40;</span>substr<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; substr_count +<span
style="color: #66cc66;">=</span> found</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> found <span
style="color: #66cc66;">&gt;</span> <span
style="color: #ff4500;">0</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; lines_substr_count +<span
style="color: #66cc66;">=</span> <span
style="color: #ff4500;">1</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; msg +<span
style="color: #66cc66;">=</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>found<span
style="color: black;">&#41;</span> + <span
style="color: #483d8b;">&quot;: &quot;</span> + line</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">elif</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; &nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: black;">&#40;</span><span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>found<span
style="color: black;">&#41;</span> + <span
style="color: #483d8b;">&quot;: &quot;</span> + line<span
style="color: black;">&#41;</span>.<span
style="color: black;">replace</span><span
style="color: black;">&#40;</span><span
style="color: #483d8b;">&quot;n&quot;</span><span
style="color: #66cc66;">,</span><span
style="color: #483d8b;">&quot;&quot;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; f.<span
style="color: black;">close</span><span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">if</span> options.<span
style="color: black;">verbose</span> <span
style="color: #ff7700;font-weight:bold;">and</span> <span
style="color: #ff7700;font-weight:bold;">not</span> options.<span
style="color: black;">flush</span>:</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; &nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> msg</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Lines read from file: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>lines_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Lines with substring found: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>lines_substr_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">print</span> <span
style="color: #483d8b;">&quot;Total substrings detected: &quot;</span><span
style="color: #66cc66;">,</span> <span
style="color: #008000;">str</span><span
style="color: black;">&#40;</span>substr_count<span
style="color: black;">&#41;</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp; <span
style="color: #ff7700;font-weight:bold;">return</span></div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li
style="font-weight: normal; vertical-align:top;"><div
style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span
style="color: #ff7700;font-weight:bold;">if</span> __name__ <span
style="color: #66cc66;">==</span> <span
style="color: #483d8b;">&quot;__main__&quot;</span>: &nbsp;main<span
style="color: black;">&#40;</span><span
style="color: black;">&#41;</span></div></li></ol></div></div><p><a
class="a2a_button_citeulike" href="https://www.addtoany.com/add_to/citeulike?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="CiteULike" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Pocket" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_kindle_it" href="https://www.addtoany.com/add_to/kindle_it?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Kindle It" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_evernote" href="https://www.addtoany.com/add_to/evernote?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Evernote" rel="nofollow noopener" target="_blank"></a><a
class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&amp;linkname=Simple%20substring%20counting%20script%20in%20Python" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a
class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fbogdan.org.ua%2F2006%2F06%2F21%2Fsimple-substring-counting-script-in-python.html&#038;title=Simple%20substring%20counting%20script%20in%20Python" data-a2a-url="https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html" data-a2a-title="Simple substring counting script in Python"><img
src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded> <wfw:commentRss>https://bogdan.org.ua/2006/06/21/simple-substring-counting-script-in-python.html/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> </channel> </rss>