<?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>firsttube.com &#187; PHP</title>
	<atom:link href="http://firsttube.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://firsttube.com</link>
	<description>crunchy nuggets, served semi-daily</description>
	<lastBuildDate>Wed, 10 Mar 2010 03:38:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<atom:link rel='hub' href='http://firsttube.com/?pushpress=hub'/>
		<item>
		<title>Behind the Scenes at OSNews</title>
		<link>http://firsttube.com/read/behind-the-scenes-at-osnews/</link>
		<comments>http://firsttube.com/read/behind-the-scenes-at-osnews/#comments</comments>
		<pubDate>Mon, 04 May 2009 22:55:57 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[OSNews]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1279</guid>
		<description><![CDATA[I just started putting together a series of articles I will be publishing on OSNews.  I&#8217;ve only roughly sketched it out, but in short, it&#8217;s going to discuss how OSNews works, how the PHP is structured, why we made certain architectural decisions, why we don&#8217;t use tried-and-true CMSes like WordPress, Slash, or Joomla!, and how, [...]]]></description>
			<content:encoded><![CDATA[<p>I just started putting together a series of articles I will be publishing on OSNews.  I&#8217;ve only roughly sketched it out, but in short, it&#8217;s going to discuss how <a href='http://osnews.com'>OSNews</a> works, how the PHP is structured, why we made certain architectural decisions, why we don&#8217;t use tried-and-true CMSes like WordPress, Slash, or Joomla!, and how, during peak traffic times, we have survived 30,000 unique visitors per hour on a single server.  <a href='http://osnews.com'>OSNews</a> didn&#8217;t happen by mistake: over a series of months, arguably years, we took a constant read of performance, hits, server load, and usability with the mission to continually improve load time, performance, and <abbr title="user experience">UX</abbr>.  We&#8217;ve just recently begun testing some new data presentation methods that I intend to include in my little exposé.</p>
<p>If you&#8217;re interested in some revealing behind-the-scenes info, feel free to ask questions now.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/behind-the-scenes-at-osnews/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flip 3.0.1</title>
		<link>http://firsttube.com/read/flip-301/</link>
		<comments>http://firsttube.com/read/flip-301/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 20:52:05 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flip]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1229</guid>
		<description><![CDATA[I never expected to release another version of my old weblog project Flip, but while searching my own name in a new search engine, I came upon several vulnerability reports for Flip 3.0.  I&#8217;ve known about them for awhile now, but having dropped Flip in favor of another project (which I&#8217;ve since abandoned, for [...]]]></description>
			<content:encoded><![CDATA[<p>I never expected to release another version of my old weblog project <a href="http://firsttube.com/tag/flip">Flip</a>, but while searching my own name in a new search engine, I came upon several vulnerability reports for Flip 3.0.  I&#8217;ve known about them for awhile now, but having dropped Flip in favor of another project (which I&#8217;ve since abandoned, for the most part, in favor of WordPress), it seemed pointless to bother.  However, since there is an active exploit, I thought I&#8217;d release an update and a patch.</p>
<p>I don&#8217;t believe anyone out there is still using Flip, but if there is, this is how to defeat the script: simple add this line at line 102 of account.php:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'em'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;][&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Fail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and this at line 162:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nem'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;][&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Fail'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Alternatively, you can download the modified file <a href="http://releases.smallaxesolutions.com/account.phps">here</a> or <a href="http://releases.smallaxesolutions.com/flip-3.0.1.zip">download Flip 3.0.1 here</a>.</p>
<p>It may sound odd, but I would highly recommend that you do *not* use this code.  It&#8217;s now 7 years old and the web is a much different place.  The code here is really not suited for running a website today.  That said, it was odd to unzip and install it and see that it actually works.  The rendering of most of the &#8220;themes&#8221; is weird (Fudge works great), but otherwise, everything worked.</p>
<p>If you are still a Flip user, I recommend you update your account.php page immediately, and if you have the time and inclination, upgrade to 3.0.1.  The following files have some minor changes:</p>
<ul class="list">
<li>account.php</li>
<li>index.php</li>
<li>inc/config.php</li>
<li>README.html</li>
</ul>
<p>Once again, this code is aged not particularly well suited for today&#8217;s web.  If you want a simple weblog, I recommend <a href="http://wordpress.org">WordPress</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/flip-301/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft&#8217;s Web App Gallery FAIL</title>
		<link>http://firsttube.com/read/microsofts-web-app-gallery-fail/</link>
		<comments>http://firsttube.com/read/microsofts-web-app-gallery-fail/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 17:42:42 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1210</guid>
		<description><![CDATA[Giving Microsoft, IIS, and PHP.exe the benefit of the doubt, I decided to try installing WordPress on Windows via Microsoft&#8217;s new Web Application Gallery.   The install is simple and straightforward: install MySQL, go to the web app gallery, click on the download, choose what you want, poof! Done.
I got the first few steps knocked [...]]]></description>
			<content:encoded><![CDATA[<p>Giving Microsoft, IIS, and PHP.exe the benefit of the doubt, I decided to try installing WordPress on Windows via <a href="http://www.microsoft.com/web/gallery/">Microsoft&#8217;s new Web Application Gallery</a>.   The install is simple and straightforward: install MySQL, go to the web app gallery, click on the download, choose what you want, poof! Done.</p>
<p>I got the first few steps knocked out, I selected WordPress,  gave it my MySQL username and password, and let it go.  It installed PHP for Windows, the MySQL connector, and WordPress.  Then I launched my browser and pointed to http://localhost:81 and&#8230; no.  Error 402.  I monkeyed with the site in IIS and was able to generate an error that simply says:  <em>Parameter not found</em>.</p>
<p>PHP is installed.  IIS assicates .php files with PHP.exe.  But Wordpress <em>no worky</em>.</p>
<p>Fail.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/microsofts-web-app-gallery-fail/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Posting Your Latest Tweet in Wordpress</title>
		<link>http://firsttube.com/read/posting-your-latest-tweet-in-wordpress/</link>
		<comments>http://firsttube.com/read/posting-your-latest-tweet-in-wordpress/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 16:48:31 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Social Networking]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1118</guid>
		<description><![CDATA[Although I posted yesterday how to add your latest tweet to Wordpress without a plugin, I made several changes to the script before I posted it to make it more &#8220;generic&#8221; and re-usable. Since I&#8217;ve changed it quite a bit, I decided to repost it.  This new script also autolinks @usernames and #hash tags.
Directions [...]]]></description>
			<content:encoded><![CDATA[<p>Although I posted yesterday how to add your latest tweet to Wordpress without a plugin, I made several changes to the script before I posted it to make it more &#8220;generic&#8221; and re-usable. Since I&#8217;ve changed it quite a bit, I decided to repost it.  This new script also autolinks @usernames and <a href="http://www.google.com/search?q=twitter+hash+tags">#hash tags</a>.</p>
<p>Directions are this easy: set the path of $tw_File with a static, writable file.  Set $tw_userid to your Twitter user id.  Done. </p>
<p>Download <a href="http://firsttube.com/s/latest-tweet"><strong>firsttube.com &#8220;get latest tweet&#8221; php snippet</strong></a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/posting-your-latest-tweet-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Add Latest Tweet to Wordpress (Without a Plugin)</title>
		<link>http://firsttube.com/read/how-to-add-latest-tweet-to-wordpress/</link>
		<comments>http://firsttube.com/read/how-to-add-latest-tweet-to-wordpress/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 20:18:07 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[Social Networking]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1101</guid>
		<description><![CDATA[I decided to add my latest &#8220;tweet&#8221; from Twitter to the sidebar of my Wordpress blog.  Rather than use yet another plugin that adds yet another hook &#8211; and there are many that do this with lots of code, I decided to use a homegrown solution, dependant only on PHP4+ and cURL  (most webhosts [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to add my latest &#8220;tweet&#8221; from Twitter to the sidebar of my Wordpress blog.  Rather than use yet another plugin that adds yet another hook &#8211; and there are many that do this with lots of code, I decided to use a homegrown solution, dependant only on PHP4+ and cURL  (most webhosts already have cURL compiled in, if not, you should request it).  Adding the following to any of the files in your Wordpress theme will print out your current Twitter status and cache the results so you don&#8217;t hammer their system.</p>
<p>First, <a href="http://help.twitter.com/forums/23786/entries/15360">snag your Twitter user id</a>.  Then, open up your theme file.  I put mine in sidebar.php found in <em>/wp-content/themes/&lt;THEMENAME&gt;/</em>.    Use the below code.  If you want the output wrapped in a list, you would need to put &lt;ul&gt; and &lt;li&gt; tags around this code.</p>
<p>Carefully set your variables.  The cache file should be writable.  Note that you can use a decimal value for $tw_BlankAfter and $tw_Minutes if necessary.   That&#8217;s it.</p>
<p><em>Due to what must be a bug in Wordpress, please ignore the closing &#8220;&lt;/text&gt;&lt;/created_at&gt;&#8221; at the end of this post.  It&#8217;s trying be smart and &#8220;fix&#8221; broken tags, but the code is right. </em></p>
<p><strong>NOTE (2/20/09): I have updated the below code.  The new version can be found at &#8220;<a href="http://firsttube.com/read/posting-your-latest-tweet-in-wordpress/">Posting Your Latest Tweet in Wordpress</a>&#8220;. </strong><em><br />
</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* ~~~~ Custom Twitter Bit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */</span>
<span style="color: #666666; font-style: italic;">/* ~~~~ Adam S, firsttube.com, twitter @sethadam1 ~~~~~~~~~~~~~~~~~~~~ */</span>
&nbsp;
<span style="color: #000088;">$tw_File</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/path/to/a/static/writable/file/twitter.html'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tw_Userid</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'XXXXXXX'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//set to your Twitter user id</span>
<span style="color: #000088;">$tw_BlankAfter</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//blank out status if it's older than this many days</span>
<span style="color: #000088;">$tw_Minutes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//minutes between reloads</span>
&nbsp;
<span style="color: #000088;">$tw_Offset</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//leave as is</span>
<span style="color: #666666; font-style: italic;">// uncomment below time if you want to allow a manual reset via ?twitter-reset</span>
<span style="color: #666666; font-style: italic;">// if($_SERVER['QUERY_STRING']=='twitter-reset') { $tw_Offset=0; } </span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Do not edit below this line */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_File</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span>time<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_Offset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">include</span> <span style="color: #000088;">$tw_File</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_writable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_File</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$tw_iswritable</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$tw_time</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">86400</span><span style="color: #339933;">*</span><span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_BlankAfter</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_Offset</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$tw_time</span><span style="color: #339933;">=</span><span style="color: #000088;">$tw_Offset</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$tw_hyperlinks</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$tw_c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_c</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;http://twitter.com/statuses/user_timeline/&quot;</span>
		<span style="color: #339933;">.</span><span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_Userid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_c</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$tw_src</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(.*)&amp;lt; \/created_at&amp;gt;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tw_src</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tw_d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000088;">$tw_time</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(.*)&amp;lt; \/text&amp;gt;/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tw_src</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tw_m</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tw_status</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;amp;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&amp;amp;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$tw_m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$tw_hyperlinks</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$tw_status</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;[[:alpha:]]+://[^&amp;lt;&amp;gt;[:space:]]+[[:alnum:]/]&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">&quot;&lt;a href=&quot;</span>\<span style="color: #0000ff;">&quot;&gt;<span style="color: #000099; font-weight: bold;">\\</span>0&lt;/a&gt;&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #000088;">$tw_status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$tw_output</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tw_status</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_iswritable</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_File</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span> 
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_iswritable</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tw_File</span><span style="color: #339933;">,</span><span style="color: #000088;">$tw_output</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$tw_output</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">/* ~~~ /Custom Twitter Bit ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */</span></pre></div></div>

<p><small>Please note that portions of this code come from the twtter_status() function that was not written by me, but is available from <a href="http://www.google.com/search?q=function+twitter_status()">various sources online</a>.</small></p>
<p><strong>Update</strong>: Removed function and put code inline.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/how-to-add-latest-tweet-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Sorting a Multi-Dimensional Array with PHP</title>
		<link>http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/</link>
		<comments>http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 18:52:48 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://firsttubecom/?p=1044</guid>
		<description><![CDATA[Every so often I find myself with a multidimensional array that I want to sort by a value in a sub-array.  I have an array that might look like this:

//an array of some songs I like
$songs =  array&#40;
		'1' =&#62; array&#40;'artist'=&#62;'The Smashing Pumpkins', 'songname'=&#62;'Soma'&#41;,
		'2' =&#62; array&#40;'artist'=&#62;'The Decemberists', 'songname'=&#62;'The Island'&#41;,
		'3' =&#62; array&#40;'artist'=&#62;'Fleetwood Mac', 'songname' =&#62;'Second-hand [...]]]></description>
			<content:encoded><![CDATA[<p>Every so often I find myself with a multidimensional array that I want to sort by a value in a sub-array.  I have an array that might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//an array of some songs I like</span>
<span style="color: #000088;">$songs</span> <span style="color: #339933;">=</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'artist'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Smashing Pumpkins'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'songname'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Soma'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'artist'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Decemberists'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'songname'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'The Island'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'artist'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Fleetwood Mac'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'songname'</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">'Second-hand News'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The problem is thus: I&#8217;d like to echo out the songs I like in the format &#8220;<em>Songname</em> (<em>Artist</em>),&#8221; and I&#8217;d like to do it alphabetically by artist.  PHP provides many functions for sorting arrays, but none will work here.  <em>ksort()</em> will allow me to sort by key, but the keys in the <em>$songs</em> array are irrelevant.  <em>asort()</em> allows me to sort and preserves keys, but it will sort $songs by the value of each element, which is also useless, since the value of each is &#8220;array()&#8221;.  <em>usort()</em> is another possible candidate and can do multi-dimensional sorting, but it involves building a callback function and is often pretty long-winded.  Even the examples in the PHP docs references specific keys. </p>
<p>So I developed a quick function to sort by the value of a key in a sub-array.  Please note this version does a case-insensitive sort. See subval_sort() below.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> subval_sort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$subkey</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$subkey</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">asort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To use it on the above, I would simply type:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$songs</span> <span style="color: #339933;">=</span> subval_sort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'artist'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is what you should expect see:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>artist<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Fleetwood Mac
            <span style="color: #009900;">&#91;</span>song<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Second<span style="color: #339933;">-</span>hand News
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>artist<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> The Decemberists
            <span style="color: #009900;">&#91;</span>song<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> The Island
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>artist<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> The Smashing Pumpkins
            <span style="color: #009900;">&#91;</span>song<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> Cherub Rock
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The songs, sorted by artist. </p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/sorting-a-multi-dimensional-array-with-php/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>firsttube.com Upgraded To Wordpress 2.7</title>
		<link>http://firsttube.com/read/firsttubecom-upgraded-to-wordpress-27/</link>
		<comments>http://firsttube.com/read/firsttubecom-upgraded-to-wordpress-27/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 20:28:56 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Scoble]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://firsttubecom/?p=997</guid>
		<description><![CDATA[So far, one problem, two gripes.  My problem is that I can&#8217;t seem to get posts with dots in the slug title to work right, even though I once solved this problem before.  What&#8217;s worse is that it won&#8217;t fetch those posts anymore, which really sucks.
Onto my gripes. I can&#8217;t get inline replying/threading to work. [...]]]></description>
			<content:encoded><![CDATA[<p>So far, one problem, two gripes.  My problem is that I can&#8217;t seem to get posts with dots in the slug title to work right, even though I once <a href="http://firsttube.com/read/hacking-wordpress-day-two/">solved this problem before</a>.  What&#8217;s worse is that it won&#8217;t fetch those posts anymore, which really sucks.</p>
<p>Onto my gripes. I can&#8217;t get inline replying/threading to work.  There is very little documentation on it so far.  The functions are called comment_reply_link() and get_comment_reply_link(), and there&#8217;s nothing anywhere in the codex that helps, there&#8217;s little on the internet, the only place to get any real detail is the code itself, which explains:</p>
<p><em><small>from wp-includes/comment-template.php starting at line 949 on WP 2.7.0</small></em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #339933;">*</span> Retrieve HTML content <span style="color: #b1b100;">for</span> reply to comment <span style="color: #990000;">link</span><span style="color: #339933;">.</span>
 <span style="color: #339933;">*</span>
 <span style="color: #339933;">*</span> The <span style="color: #b1b100;">default</span> arguments that can be override are <span style="color: #0000ff;">'add_below'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'respond_id'</span><span style="color: #339933;">,</span>
 <span style="color: #339933;">*</span> <span style="color: #0000ff;">'reply_text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'login_text'</span><span style="color: #339933;">,</span> and <span style="color: #0000ff;">'depth'</span><span style="color: #339933;">.</span> The <span style="color: #0000ff;">'login_text'</span> argument will be
 <span style="color: #339933;">*</span> used<span style="color: #339933;">,</span> <span style="color: #b1b100;">if</span> the user must <span style="color: #990000;">log</span> in or register first before posting a comment<span style="color: #339933;">.</span> The
 <span style="color: #339933;">*</span> <span style="color: #0000ff;">'reply_text'</span> will be used<span style="color: #339933;">,</span> <span style="color: #b1b100;">if</span> they can post a reply<span style="color: #339933;">.</span> The <span style="color: #0000ff;">'add_below'</span> and
 <span style="color: #339933;">*</span> <span style="color: #0000ff;">'respond_id'</span> arguments are <span style="color: #b1b100;">for</span> the JavaScript moveAddCommentForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">function</span>
 <span style="color: #339933;">*</span> parameters<span style="color: #339933;">.</span>
 <span style="color: #339933;">*</span>
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>since 2<span style="color: #339933;">.</span>7<span style="color: #339933;">.</span>0
 <span style="color: #339933;">*</span>
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>param <span style="color: #990000;">array</span> <span style="color: #000088;">$args</span> Optional<span style="color: #339933;">.</span> Override <span style="color: #b1b100;">default</span> options<span style="color: #339933;">.</span>
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>param int <span style="color: #000088;">$comment</span> Optional<span style="color: #339933;">.</span> Comment being replied to<span style="color: #339933;">.</span>
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span>param int <span style="color: #000088;">$post</span> Optional<span style="color: #339933;">.</span> Post that the comment is going to be displayed on<span style="color: #339933;">.</span>
 <span style="color: #339933;">*</span> <span style="color: #339933;">@</span><span style="color: #b1b100;">return</span> string<span style="color: #339933;">|</span>bool<span style="color: #339933;">|</span>null <span style="color: #990000;">Link</span> to show comment form<span style="color: #339933;">,</span> <span style="color: #b1b100;">if</span> successful<span style="color: #339933;">.</span> <span style="color: #009900; font-weight: bold;">False</span><span style="color: #339933;">,</span> <span style="color: #b1b100;">if</span> comments are closed<span style="color: #339933;">.</span></pre></div></div>

<p>It doesn&#8217;t matter much, because it doesn&#8217;t work, period, even though I&#8217;ve followed the instruction here to a t.  So I&#8217;ll have to fix that in time.</p>
<p>My last gripe is with the new wp_list_comments() routine.  I understand this is all new, but the idea that templating comments requires a callback function as a wrapper to all comments, pings, and trackbacks is <strong>clumsy</strong> at best.  <a href="http://codex.wordpress.org/Template_Tags/wp_list_comments">The codex on wp_list_comments()  have nothing</a> to explain it to people, so while I&#8217;ve dug in and gotten things working, it&#8217;s not for the feint of heart just yet, since you need to build a PHP function in your theme in your <em>functions.php</em> file (or create one if it doesn&#8217;t exist, which cannot be done via the Dashboard).  I&#8217;m a little sad, since the theme system is so flexible and the new plugin system is just incredible, to see the new comment loop be so <em>manual</em> compared to the single file approach used so successfully in the past.</p>
<p>I know that <a href="http://scobleizer.com/2008/12/12/wordpress-27/">Scoble says Wordpress 2.7 rocks</a>, and it does.  Scoble doesn&#8217;t realize the shortcomings because he hasn&#8217;t tried to play with the new features, and fortunately, it very gracefully degrades.  But it&#8217;s got some work to do to be perfect, for me at least.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/firsttubecom-upgraded-to-wordpress-27/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress 2.7 RC1</title>
		<link>http://firsttube.com/read/wordpress-27-rc1/</link>
		<comments>http://firsttube.com/read/wordpress-27-rc1/#comments</comments>
		<pubDate>Tue, 02 Dec 2008 18:42:27 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://firsttubecom/?p=983</guid>
		<description><![CDATA[I just downloaded and installed WordPress 2.7 RC1.  The upgrade took about 3 minutes, end to end, and the &#8220;several moments&#8221; database upgrade took less than 2 seconds.  All in the all, there&#8217;s very little to notice on the front end that is different, I haven&#8217;t been able to test comment threading yet. [...]]]></description>
			<content:encoded><![CDATA[<p>I just downloaded and installed WordPress 2.7 RC1.  The upgrade took about 3 minutes, end to end, and the &#8220;several moments&#8221; database upgrade took less than 2 seconds.  All in the all, there&#8217;s very little to notice on the front end that is different, I haven&#8217;t been able to test comment threading yet.  However, the new admin site is really nice looking.  The Dashboard is a HUGE improvement over the &lt;2.7 series.</p>
<p>Themes were entirely unbroken.  Upgrading <a href='http://firsttube.com'>firsttube.com</a> may be a bit more of a challenge since <a href="http://firsttube.com/read/hacking-wordpress-day-two/">I&#8217;ve manually changed a few fore WordPress files</a>, which may prevent in place automatic upgrades.  However, all in all, I think the 2.7 release is looking really great.  </p>
<p>When 2.7 final is released, I expect to be updating my live site pretty quickly.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/wordpress-27-rc1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Weirdness</title>
		<link>http://firsttube.com/read/php-weirdness/</link>
		<comments>http://firsttube.com/read/php-weirdness/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 19:46:03 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Nerd]]></category>
		<category><![CDATA[Yahoo!]]></category>

		<guid isPermaLink="false">http://firsttubecom/?p=660</guid>
		<description><![CDATA[Beware: this post is definitely not for the feint of heart.  It includes a lot of code.  You have been warned.
I wrote an application some time ago for my company that looks up the longitude and latitude of an address for use in our geocoding initiative.  It relied on yahoo_geo(), a function [...]]]></description>
			<content:encoded><![CDATA[<p>Beware: this post is definitely not for the feint of heart.  It includes a lot of code.  You have been warned.</p>
<p>I wrote an application some time ago for my company that looks up the longitude and latitude of an address for use in our geocoding initiative.  It relied on yahoo_geo(), <a href="http://toys.lerdorf.com/archives/2005/11.html">a function written by PHP creator Rasmus Lerdorf</a> and the Yahoo Maps API.  It was largely dependent on this function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> yahoo_geo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&amp;amp;location='</span>
		<span style="color: #339933;">.</span><span style="color: #990000;">rawurlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/tmp/yws_geo_'</span><span style="color: #339933;">.</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	request_cache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">43200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">libxml_use_internal_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tmp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'precision'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Result<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'precision'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>Result<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>children<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</p>
<p>This function worked for over two years for us with no problems at all.  Then suddenly, in the last month, it started getting spotty.  I fixed things by commenting out the caching parts of the function and forcing each execution to run again.  Then I got errors about the libxml_use_internal_errors() function, so I commented that out.  But today, the function just flat out failed, every single time returning the same error: </p>
<p><strong>Warning: file_get_contents(http://XXXXXXXXXX/XXX) [function.file-get-contents]: failed to open stream: HTTP request failed! in /home/intranet/html/fetch.php on line X</strong></p>
<p>What the heck? This code is all over the web.  I&#8217;ve tried a million permutations of this function, including using fopen() and ob_get_contents(), and none have worked.  And most frustratingly, I could load the URL successfully in Lynx and eLinks, so the machine could quickly and easily fetch the URL.</p>
<p>So I ventured into a sandbox I&#8217;ve never really played before: <a href="http://us2.php.net/manual/en/ref.curl.php">cURL</a>.   cURL is an interesting animal.  But the interesting thing is, once I got it working, it worked faster than ever! So, without further ado, here is the new and improved yahoo_geo() function: </p>
<p>
<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> yahoo_geo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://api.local.yahoo.com/MapsService/V1/geocode?appid=rlerdorf&amp;#038;location='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$stream</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stream</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stream</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'precision'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Result</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'precision'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span>  <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</p>
<p><small><strong>Note:</strong> If you&#8217;re reproducing these functions elsewhere, be careful &#8211; WordPress may have converted the quotes into smart quotes that will need to be fixed before this script will run properly. </small></p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/php-weirdness/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OSNews vs. WordPress</title>
		<link>http://firsttube.com/read/osnews-vs-wordpress/</link>
		<comments>http://firsttube.com/read/osnews-vs-wordpress/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 00:35:20 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Nerd]]></category>
		<category><![CDATA[OSNews]]></category>
		<category><![CDATA[Websites]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://firsttubecom/?p=656</guid>
		<description><![CDATA[I&#8217;ve spent quite a bit of time, over the last 5 or 6 days, diving into WordPress and learning what makes it tick.  Parts of WordPress are really impressive &#8211; just flat out cool. The way some of it works is fairly complex and deciphering it sometimes means reading page after page after page to understand an [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent quite a bit of time, over the last 5 or 6 days, diving into WordPress and learning what makes it tick.  Parts of WordPress are <em>really</em> impressive &#8211; just flat out <em><strong>cool</strong></em>. The way some of it works is fairly complex and deciphering it sometimes means reading page after page after page to understand an entire routine.  But sometimes, when you finally see, end to end, how something in WordPress works &#8211;  I mean really see individual bits of the engine &#8211; you have to admit it teaches you a little about PHP.  WordPress, underneath it all, is a pretty big beast and its strength and ubiquitous presence comes largely, I think, from the fact that it can do virtually anything.  The really sweet plugin system, the ways hooks work, &#8220;The Loop,&#8221; the dynamic options panel &#8211; it&#8217;s all very educational.  </p>
<p>The interesting thing here is that I&#8217;ve browsed the source of <a href="http://slashcode.com">Slash</a>, <a href="http://scoop.kuro5hin.org">Scoop</a>, <a href="http://phpnuke.org">phpNuke</a>, and now <a href="http://wordpress.org">WordPress</a>, and all of them are <strong>definitively</strong> more complex and much heavier than the entire <a href='http://osnews.com'>OSNews</a> codebase. Now, before you jump all over me &#8211; firstly, Slash and Scoop are Perl, and I don&#8217;t really read Perl, so I can&#8217;t speak as an expert there.  Secondly, WordPress and Nuke both are very portable and dynamic, whereas <a href='http://osnews.com'>OSNews</a> has a narrow focus and, location-wise, is very static.  But that aside, <a href='http://osnews.com'>OSNews</a> has withstood simultaneous link bombs from <a href="http://slashdot.org">Slashdot</a> and <a href="http://digg.com">Digg</a>.  As amazing as WordPress is, it&#8217;s mostly amazing that it functions at all and loads in less than 2 minutes per page with as much going on as I can see behind the scenes.   That&#8217;s not a cut on WordPress, by the way.</p>
<p>In fact, if anything , what is really impressed upon me is how smooth and simple <a href='http://osnews.com'>OSNews</a> code is, if I may be so bold.  OSNews runs superfast due, in part, to lots of creative caching, some on-demand, some via cron.  But it also does so because of highly efficient queries that are measured for speed on their JOINs, meaning in some cases, it&#8217;s faster to do 20 simple queries than one complex one, or build a long and scary chain of &#8220;OR x=a OR x=b OR x=c OR x=d&#8230;&#8221;  Watching WordPress code in action is really fun for me, but watching <a href='http://osnews.com'>OSNews</a> work knowing what I now know about how much work PHP can cram into its threads is even more fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/osnews-vs-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>An Argument for PHP</title>
		<link>http://firsttube.com/read/An-Argument-for-PHP/</link>
		<comments>http://firsttube.com/read/An-Argument-for-PHP/#comments</comments>
		<pubDate>Sun, 11 May 2008 19:46:11 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[Nerd]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://firsttubecom/read/An-Argument-for-PHP</guid>
		<description><![CDATA[Currently, over on Slashdot, there is an article on forthcoming features in PHP version 6.  And, like most PHP articles, the comments section is flooded with jackasses arguing that PHP sucks as a language.  I get frustrated by the entire &#8220;PHP sucks&#8221; campaign, largely because it&#8217;s like the HTML e-mail argument &#8211; mostly [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, over on <a href="http://slashdot.org">Slashdot</a>, there is an article on <a href="http://tech.slashdot.org/article.pl?sid=08/05/11/1939240">forthcoming features in PHP version 6</a>.  And, like most PHP articles, the comments section is flooded with jackasses arguing that PHP sucks as a language.  I get frustrated by the entire &#8220;PHP sucks&#8221; campaign, largely because it&#8217;s like the HTML e-mail argument &#8211; mostly driven by the fact that it&#8217;s stylish to hate them &#8211; but I&#8217;m going to go further.  I argue than <i>everyone</i> posting about how PHP is a <i>bad language as a whole</i> is <strike>an idiot. Every single one.  Each is a foolish, arrogant, nerd <b>sheep</b> who can&#8217;t think for themselves</strike>. <span style="color:#777;"><b>Update 5/14/08 20:39 UTC</b>: Okay, this piece was linked by several sources, and the truth is, I had just read some George Carlin, so I was probably more aggressive than I intended to be.  What I really mean is that people posting about how PHP is a <i>bad language as a whole</i> without citing any reasons are generally following a trend, trying to look cool, or too narrow-minded to be considered credible.  And the responses I&#8217;ve seen across the net have, thus far, supported this argument. </span></p>
<p>Why?  Let&#8217;s argue for a second that everything people say about PHP is true, as many of the complaints are sound.  </p>
<p>It&#8217;s true the primary namespace has way too many functions &#8211; over three thousand, I&#8217;m told.  It&#8217;s true that the function names are inconsistent, some have underscores, some don&#8217;t.  It&#8217;s true that the function names are often verbose.  It&#8217;s true that OOP was weak until recently, it&#8217;s true that <i>register_globals</i> was a security nightmare.  All those things are potential issues, and all languages have them.  As the &#8220;real programmers&#8221; who write Perl would never admit, reading other people&#8217;s terse Perl is often a f&#8217;ing disaster, even for seasoned Perl-ites.  And when using compiled ASP.net &#8211; for best performance, natch &#8211; you must update your entire site (well, all the concerned ASPX pages and DLLs) to make elementary changes.  </p>
<p>That said, PHP is easy.  Really easy.  And it&#8217;s a trivial task to get a website up and running fairly quickly.  And you can serve enormous amounts of traffic as proven not only by <a href='http://osnews.com'>OSNews</a> (who have been dugg and Slashdotted concurrently), but by Yahoo!, Wikipedia, Flickr, Facebook, and many, many others.   And why are there <i>so many</i> open source PHP frameworks, apps, CMSes, etc? Because PHP is installable virtually everywhere, it&#8217;s very portable, and it&#8217;s really simple to hack up.  Try installing something dependent on mod_perl (e.g. <a href="http://slashcode.com">Slash</a> or <a href="http://scoop.kuro5hin.org">Scoop</a>) and get back to me on the ease of the install.  </p>
<p>The fact is, even if everyone&#8217;s fears about writing insecure code is true, the ability to make mistakes does not mean everyone does, and those who would forsake &#8220;<b>the right tool for the job at hand</b>&#8221; shouldn&#8217;t be trusted even to water your plants, because they are obviously nitwits.  If you can&#8217;t concede that PHP can be the right tool <i>some</i> of the time for <i>some</i> situations, you shouldn&#8217;t be trusted to code or make adult decisions.  No, I argue that the reason they dislike PHP is because many start with PHP and thus, admitting to liking it would make them appear to be a &#8220;noob.&#8221; It&#8217;s because they must appear to be seasoned pros.  It&#8217;s the bragging rights on the 21st century.  </p>
<p>Nobody has ever claimed PHP is the solution to everything, but it is a remarkably easy tool for scripting dynamically generated HTML.  And, in my opinion and experience, it does so better than Perl, better than Ruby, and a hell of a lot better than both ASP.net and JSP.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/An-Argument-for-PHP/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>HAXX0RED</title>
		<link>http://firsttube.com/read/HAXX0RED/</link>
		<comments>http://firsttube.com/read/HAXX0RED/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 10:41:38 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://firsttubecom/read/HAXX0RED</guid>
		<description><![CDATA[So, I updated firsttube.com to &#8220;revision 9&#8243; on Friday, and when I went to show someone last night, imagine my surprise when I found the whole thing hosed.  The site was missing entire chunks &#8211; random, non-sequential directories, missing entirely.  
I&#8217;ll spare you the details: I got hacked.  Someone either brute forced [...]]]></description>
			<content:encoded><![CDATA[<p>So, I updated <a href='http://firsttube.com'>firsttube.com</a> to &#8220;revision 9&#8243; on Friday, and when I went to show someone last night, imagine my surprise when I found the whole thing hosed.  The site was missing entire chunks &#8211; random, non-sequential directories, missing entirely.  </p>
<p>I&#8217;ll spare you the details: I got hacked.  Someone either brute forced their way into the admin site (which is now pretty locked down, until I figure this all out) or brute forced into SSH and uploaded several malicious PHP scripts.  They are scary, I actually have them intact in a backup from a few days ago.  How much has been revealed? My MySQL passwords? It&#8217;s impossible to tell.  Virtually everything will need scrubbing.</p>
<p>In the meantime, excuse any wonkiness until all is repaired.  The good news is this finally forces me to finish work on the new administrative area I&#8217;ve been playing with.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/HAXX0RED/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To REALLY Survive Digg on a Shared Host</title>
		<link>http://firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/</link>
		<comments>http://firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 22:48:29 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Digg]]></category>
		<category><![CDATA[High Performance]]></category>

		<guid isPermaLink="false">http://firsttubecom/read/How-To-REALLY-Survive-Digg-on-a-Shared-Host</guid>
		<description><![CDATA[After reading a ridiculous post on &#8220;surviving the Digg effect on a shared host,&#8221; (and then laughing ridiculously at it), I decided to write a real tutorial on real-live ways not only to survive the Digg effect, but also a simple but powerful way to improve your site&#8217;s performance.  Read more within.

The fact is, [...]]]></description>
			<content:encoded><![CDATA[<p>After reading a ridiculous post on &#8220;<a href="http://digg.com/programming/How_to_Survive_Digg_on_a_Shared_Host">surviving the Digg effect on a shared host</a>,&#8221; (and then laughing ridiculously at it), I decided to write a <i>real</i> tutorial on real-live ways not only to survive the Digg effect, but also a simple but powerful way to improve your site&#8217;s performance.  Read more within.</p>
<p><span id="more-92"></span><br />
The fact is, it&#8217;s rarely PHP that crashes a website.  It&#8217;s usually overloading MySQL and/or bandwidth limitations.  Obviously, we can&#8217;t do much about bandwidth.  If you&#8217;re on a really cool host, they may be psyched for your Digging or Slashdotting, but most are not particularly keen on you using too much processor, too much bandwidth, or too many MySQL connections.  Although MySQL is capable of amazing scaling, rarely do web hosts leverage it properly, and even if they did, rarely do most programmers know how to read into slow queries, find inefficient code like loops within loops, and debug a site performance before it&#8217;s too late.  </p>
<p>The site linked from Digg shares a very lame trick, let&#8217;s examine:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//put this code at the very beginning of your page, before anything else</span>
<span style="color: #000088;">$randomdigg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$randomdigg</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Thanks for visiting, but the site is under a great deal of 
stress due to being on the front page of Digg. 
Please try back in a few moments. Thanks!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, so essentially, approximately 25% of the time, the page will work, the other 75% of the time, the user will get the message and be asked to refresh later.  That&#8217;s really lame.  First off, unless you want your website to be functional only 25% of the time, you have to be there babysitting it when it hits front page.  Also, you need to remove this code.  But what if you wanted everything to happen automagically? What if your site could withstand a digging, a slashdotting, a redditing, an OSNews&#8217;ing, a Farking, whatever&#8230;. all at once, without being forewarned?   </p>
<p>Although there are lots of [[http://www.danga.com/memcached/|tools that can do magic for you]], you can leverage plain old PHP to do this  dirty work for you, and it&#8217;s easy to integrate into your site.  </p>
<p>Here&#8217;s the premise, we examine the referrer of the page, and if the page is from one or many domains that you specify, you build a cache ON REQUEST.  Then, you serve the cached version.  When the cache stales, say after minute or two, you refresh it, as needed, again, on request. And the best part is, it&#8217;s easy to do. </p>
<p>So, let&#8217;s review the code.  First, at the top of the page, let&#8217;s do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$rfr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REFERER'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//get the referring site</span>
<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strstr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rfr</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'digg.com'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$op</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;do-cache&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> 
<span style="color: #000088;">$cache</span><span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/path/to/mysite/cache/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$postid</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.html&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s take it line by line.  First, we get the referring site.  If it is digg.com, we&#8217;re going to turn the cache on by setting $op to &#8220;do-cache.&#8221; Note that we preset the $op variable to null to prevent abuse.  Next, we define the file we&#8217;re going to use as the cache.  Since we might want to do this for any entry/page, we&#8217;ll give it a unique address.  Remember that you do not have to use .html as an extension, since it&#8217;s static code anyway. I&#8217;ve seen &#8220;id.cache&#8221;, &#8220;id.txt&#8221;, and even just &#8220;id&#8221;.  It doesn&#8217;t matter, it will be included in a PHP page and spit out as X/HTML anyway.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fmt</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">180</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: http://mysite.com/cache/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fmt</span> <span style="color: #339933;">&gt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1800</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
         <span style="color: #666666; font-style: italic;">//cache will reload if it's been reloaded </span>
         <span style="color: #666666; font-style: italic;">//in the last half hour</span>
	   <span style="color: #000088;">$op</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;do-cache&quot;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	   <span style="color: #000088;">$op</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This might seem overwhelming, but it&#8217;s really not.  Check it out.  If the cache exists, regardless of referrer, we&#8217;re going to check it.  The filemtime() function will tell us when the cache was last refreshed.  If it reports a time in the last 180 seconds (you can change that number), it will redirect you to the cache.  PHP will barely have to do any work, Apache will barely have to do any work, and MySQL will not be touched at all.  Almost any host can hang in there to serve static traffic like this.  </p>
<p>But what about if the cache is old?  Once your site is no longer getting hits every three minutes, for the next 27 minutes, it will blindly reload the cache.  It will keep the cache going as long as the page has been hit in the last 30 minutes.  After no one hits the cache for 30 minutes, it will go back to fully dynamic.  When someone hits the page from digg again, it will start the caching process again.  In this case, we&#8217;re going to let the cache rebuild.  Enter the next part of the code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;do-cache&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">ob_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If the $op variable is set to &#8220;do-cache&#8221;, our mission is&#8230; uh&#8230; to build or rebuild the cache. So, we begin with two simple PHP standard functions, <i>ob_clean()</i> and <i>ob_start()</i>.  The &#8220;ob&#8221; in these functions stands for &#8220;output buffering.&#8221;  When you buffer your output, nothing is echoed by the script, but rather written to a buffer.  In short, it&#8217;s all stored up in memory for eventual dumping later.  Now we stop the caching scripts and just write our normal HTML via PHP and MySQL.    </p>
<p>At the end of the page &#8211; the very end, after your html element is closed, you&#8217;ll need this last bit.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;do-cache&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cache</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #000088;">$buffer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">@</span><span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
	<span style="color: #990000;">ob_get_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  	
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What&#8217;s up with this? Again, if we&#8217;re building the cache, we&#8217;re getting the contents of the buffer, which contains all of our page.  Then we&#8217;re opening the cache file we specified above, and lastly, dumping the contents of the buffer into the cache file.  Our final action is to clear the buffer, which is unnecessary to us now anyway.  </p>
<p>That&#8217;s it.  Your cache is reloaded, and will be served for the next three minutes, after which it will reload the cache every 3 minutes until no one hits the site for 30 minutes.  Then it will go back to dynamic.  If another digg visitor comes, it will recache and serve the cache.  Having written several types of caching mechanisms for large sites myself, I can tell you this &#8220;cache on demand&#8221; method works.  On <a href="http://www4.osnews.com">OSNews v4</a>, we use this for things like user-specific RSS files, which aren&#8217;t plausibly built via cron jobs and, frankly, we don&#8217;t want to allow access to MySQL without limit.  They reload every 60 minutes, regardless of referer.  </p>
<p>If you maintain a site and have any concern about being Dugg, I encourage you to research some sort of caching mechanism.  Your web host will thank you, and you will be thankful for the maintained uptime and high hit count.  </p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integers on the Intertubes</title>
		<link>http://firsttube.com/read/integers-on-the-intertubes/</link>
		<comments>http://firsttube.com/read/integers-on-the-intertubes/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 16:22:44 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Nerd]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://firsttubecom/read/Integers-on-the-Intertubes</guid>
		<description><![CDATA[Some time ago, I wrote an application for my company.  Like most weblets I&#8217;ve written, this used PHP and either MySQL or MSSQL for the backend.  This particular application logged all phone calls. As part of the record, it would record the caller&#8217;s account number, which is a 5 or 6 digit integer.
So, [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I wrote an application for my company.  Like most weblets I&#8217;ve written, this used PHP and either MySQL or MSSQL for the backend.  This particular application logged all phone calls. As part of the record, it would record the caller&#8217;s account number, which is a 5 or 6 digit integer.</p>
<p>So, I got a phone call from the director of our customer contact department this week.  He was concerned about the reports.  He made a decision last week that when a call came in that was a lead &#8211; in other words, a non-customer, that his people would fill the phone number from the caller ID into the account number field.  But when he ran his export reports, he found that hisn techs had entered this phone number for ALL of the calls: 429-496-7295.  That&#8217;s weird, he said.  So he called me and asked why that was.  I checked all the calls and most were from one woman, so my first instinct was &#8220;Check if her browser has autocomplete turned on&#8221;.  But he swore that he tried it too and gotten the same result.</p>
<p>I checked the database and sure enough, it was right there: 429-496-7295, in all of the fields.  So I went back to the code.  In short, I took the input from the form, and declared it like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$accountnum</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'accountnum'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pretty straightforward: explicitly declare the type.  So, I started my debugging by attempting to manually enter the data into the database.  Sure enough: the account key field showed this: 4294967295.</p>
<p>So, I went back to the PHP and started by dumping out the raw SQL query:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">INSERT INTO calls <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'4294967295'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What? So the database automatically converts it to this weird phone number and PHP does too?  Suddenly it occured to me. One of the benefits of 64-bit computing is the ability to address more memory.  There are limits to what can be done in 32-bit computing, and one is that <a href="http://en.wikipedia.org/wiki/Integer_(computer_science)">integers have a limit</a>!  In this case, a database field called &#8220;integer&#8221; is limited to numbers between -2,147,483,648 and +2,147,483,647.  It just so happens that the number is the same length as a US phone number &#8211; 10 digits.  Changing the db field to &#8220;BIGINT&#8221; allowed me to manually run the SQL query and it worked.  But the app still didn&#8217;t.</p>
<p>PHP&#8217;s int() and (int) $var syntaxes both conform to the integer limit.  So I devised a work around:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ac</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'accountnum'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ac</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$ac</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$ac</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s not gorgeous, but it will more than suffice for an internal app.  We web programmers don&#8217;t usually have to deal with big integers, so it&#8217;s totally possible that web developers would never have had occasion to handle a situation like this.  Here&#8217;s looking forward to native 64-bit for our next server, though.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/integers-on-the-intertubes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHPsuexec and My Adventure With Hostgator</title>
		<link>http://firsttube.com/read/PHPsuexec-and-My-Adventure-With-Hostgator/</link>
		<comments>http://firsttube.com/read/PHPsuexec-and-My-Adventure-With-Hostgator/#comments</comments>
		<pubDate>Wed, 11 Jul 2007 10:47:40 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[Hostgator]]></category>
		<category><![CDATA[Nerd]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPsuexec]]></category>
		<category><![CDATA[Rant]]></category>
		<category><![CDATA[Web Hosting]]></category>

		<guid isPermaLink="false">http://firsttubecom/read/PHPsuexec-and-My-Adventure-With-Hostgator</guid>
		<description><![CDATA[I left for vacation on June 28, and before doing so, I took a quick glance over firsttube.com and jotted a quick blog post about it.  firsttube.com was fully functional and officially dormant for 10 days as of June 28.  
Imagine my surprise when on Monday, my wife said, &#8220;Hey, your site isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I left for vacation on June 28, and before doing so, I took a quick glance over <a href='http://firsttube.com'>firsttube.com</a> and jotted a quick <a href="http://firsttube.com/read/Vacation">blog post</a> about it.  <a href='http://firsttube.com'>firsttube.com</a> was fully functional and officially dormant for 10 days as of June 28.  </p>
<p>Imagine my surprise when on Monday, my wife said, &#8220;Hey, your site isn&#8217;t working!&#8221; The index page worked, but none of the other pages. </p>
<p>In short, my webhost, Hostgator decided to implement PHPsuexec.  Here&#8217;s the gist of this awesome program: typically, your web server runs as the &#8220;nobody&#8221; user on a server, but you login as yourself, say your username is &#8220;jdough.&#8221;  You need to use certain tricks, like using .htaccess files and <a href="http://en.wikipedia.org/wiki/Chmod">chmodding</a> to get around certain limitations.  PHPSuexec makes php run *as you,* removing the need for world writable directories and creating a need for custom php.ini files to replace certain php directives in your .htacess files.</p>
<p>Since my site doesn&#8217;t use file extensions on most files, I used a directive called DefaultType to make everything PHP.  This stopped functioning when Hostgator made the changes on Monday.  Instead, every one of the pages that relied upon that value for parsing stopped working and started displaying HTTP error 500.  </p>
<p>When I returned into town on Sunday, I opened a high priorityt ticket with Hostgator.  An hour later, I called the support line and was told an admin would reply presently.  An hour later, I replied to my confirmation email to their email support line.  Another hour later, I called again.  After 35 minutes on the phone, they finally helped me get the pages running.  But images across the site were broken.  They were generating <i>parsing</i> errors! They were being interpretted by PHP.  Yikes! Another 25 minutes on the phone today resulted in new .htaccess files everywhere.  I should tell you that today&#8217;s phone calls were with two &#8220;gators&#8221; who were both very friendly and helped me very enthusiastically. </p>
<p>Hostgator did not email me about these changes, even though they have my email address.  They did not call me, even though they have my phone number.  They did not post anything in my control panel, even though they can.  Instead, they posted it in their own support forums and expected me to check it.  A major change to the very core of the server behavior and they simply didn&#8217;t tell me.  And as a result, my sites were down for a week plus.  So if you tried visiting <a href='http://firsttube.com'>firsttube.com</a> in that time, I&#8217;m sorry for the interruption: the view page, the print page, the comments page, and nearly every other meaningful page failed to parse. </p>
<p>If I were a business and monetized my site in any way, I would immediately cancel.  But to be fair, Hostgator has unparalleled uptime, unmatched availability, awesome tools (cpanel based), a competitive rate, and a friendly support staff.  So I decided to give them one more chance.  They have burned all the trust they gained with me, and I will not be recommending them to anyone right now, but I am not taking my business elsewhere just yet.  </p>
<p>PHPsuexec is a great tool that provides a nice security boost, but do some serious testing before you implement it.  It can dramatically alter the way your websites work.</p>
]]></content:encoded>
			<wfw:commentRss>http://firsttube.com/read/PHPsuexec-and-My-Adventure-With-Hostgator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
