<?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://www.firsttube.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.firsttube.com</link>
	<description>crunchy nuggets, served semi-daily</description>
	<lastBuildDate>Tue, 03 Jan 2012 00:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>A Guide to Base Changing For Short URLs</title>
		<link>http://www.firsttube.com/read/a-guide-to-base-changing-for-short-urls/</link>
		<comments>http://www.firsttube.com/read/a-guide-to-base-changing-for-short-urls/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 19:09:56 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Nerd]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1670</guid>
		<description><![CDATA[Some time ago, I developed a VERY simple way to fake a bit.ly-style short URL. On any server that uses any form of an integer to identify an article (either in the database or the URL), on an Apache server &#8230; <a href="http://www.firsttube.com/read/a-guide-to-base-changing-for-short-urls/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, I developed a VERY simple way to fake a bit.ly-style short URL.  On any server that uses any form of an integer to identify an article (either in the database or the URL), on an Apache server that supports mod_rewrite, you edit your .htaccess file like so:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">RewriteEngine Off
<span style="color: #000000; font-weight: bold;">&lt;</span>ifmodule mod_rewrite.c<span style="color: #000000; font-weight: bold;">&gt;</span>
RewriteEngine On
RewriteBase <span style="color: #000000; font-weight: bold;">/</span>
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_FILENAME<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>-f
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_FILENAME<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>-d
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_URI<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>^index.php$
RewriteRule . index.php <span style="color: #7a0874; font-weight: bold;">&#91;</span>NC,L<span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>ifmodule<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>This essentially tells your server to redirect anything that isn&#8217;t a file or directory to index.php.</p>
<p>Then index.php looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</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;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//this is where you either query your database for a slug or build the URL</span>
<span style="color: #000088;">$uri</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://your-site-goes-here.com/path/to/article'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$uri</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;">'HTTP/1.1 301 Moved Permanently'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Link: &lt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$uri</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&gt;; rel=shortlink&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</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: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: http://your-site-goes-here.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>How do you get your short links? That&#8217;s easy.  Just run this function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$shorturl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, this isn&#8217;t the most compact way to condense.  Obviously, this is base36, the highest PHP can go.  But what about uppercase letters? And other characters?</p>
<p>So I set out, for some reason, to build a better condenser.</p>
<p>This is the result of several hours of work, mostly wasted, on some intellectual pursuit that was more a case of simply not letting it defeat me.  A few notes: I&#8217;m quite confident that given enough time, and if I cared, I could make the code cleaner and more efficient in some places. I&#8217;m also aware that on 32 bit machines, it maxes out at the integer limit. I does support signed integers though, from min to max.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ft_str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'0123456789abcdefghijklmnopqrstuvqwxyzABCDEFGHIJKLMNOPQRSTUVQWXYZ'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># uncomment the next line if you prefer to use potentially non-URL-safe base96
</span><span style="color: #666666; font-style: italic;"># $ft_str .= '0123456789abcdefghijklmnopqrstuvqwxyzABCDEFGHIJKLMNOPQRSTUVQWXYZ_@$!#%^&amp;*()=+\|}{][,;:~'; }
</span>
<span style="color: #000088;">$powers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$p</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span><span style="color: #000088;">$p</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$powers</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ft_unconvert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><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: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pfx</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pfx</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: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$ft_str</span><span style="color: #339933;">,</span><span style="color: #000088;">$powers</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$base</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$q</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strrev</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</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;">$s</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;">$sp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ft_str</span><span style="color: #339933;">,</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$decimal</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base</span><span style="color: #339933;">,</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$sp</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pfx</span><span style="color: #339933;">.</span><span style="color: #000088;">$decimal</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ft_converter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</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: #000088;">$int</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pfx</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">;</span> <span style="color: #000088;">$int</span><span style="color: #339933;">=</span><span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#41;</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;">$pfx</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: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$ft_str</span><span style="color: #339933;">,</span><span style="color: #000088;">$powers</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$base</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$q</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">krsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$powers</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;">$powers</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</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;">$timesinto</span> <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$int</span><span style="color: #339933;">/</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$digit</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$timesinto</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$int</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$v</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;">$int</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$digit</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$int</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$int</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$int</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$pfx</span><span style="color: #339933;">.</span><span style="color: #000088;">$digit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ft_convert_demo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$ft_str</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ftc</span> <span style="color: #339933;">=</span> ft_converter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Converting &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$num</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; into base &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ft_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ftc</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br /&gt;Unconverting &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$ftc</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; to base 10: &quot;</span><span style="color: #339933;">.</span>ft_unconvert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ftc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> ft_convert_demo<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'50687'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.firsttube.com/read/a-guide-to-base-changing-for-short-urls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;ve Been Hacked! WP-Lytebox Sucks</title>
		<link>http://www.firsttube.com/read/ive-been-hacked-wp-lytebox-sucks/</link>
		<comments>http://www.firsttube.com/read/ive-been-hacked-wp-lytebox-sucks/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 21:36:16 +0000</pubDate>
		<dc:creator>Adam S</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://firsttube.com/?p=1600</guid>
		<description><![CDATA[This is the third time I&#8217;ve had to spent serious time &#8220;fixing&#8221; WordPress.  Say what you will about my old &#8220;Small Axe&#8221; or &#8220;Flip&#8221; solutions, but I never had a problem.  With WordPress, I have found regular issues. This time, &#8230; <a href="http://www.firsttube.com/read/ive-been-hacked-wp-lytebox-sucks/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img style="width: 150px; height: 150px; padding: 5px; float: right;" src="http://firsttube.com/uploads/2010/04/wordpress-hack-150x150.jpg" alt="Hacking WordPress" />This is the third time I&#8217;ve had to spent serious time &#8220;fixing&#8221; WordPress.  Say what you will about my old &#8220;Small Axe&#8221; or &#8220;Flip&#8221; solutions, but I never had a problem.  With WordPress, I have found regular issues.</p>
<p>This time, I traced the problem back to wp-lytebox, and I&#8217;m ashamed to say I&#8217;ve had to fix the same problem before.  It all started when I couldn&#8217;t load the post page in the backend of <a href='http://firsttube.com'>firsttube.com</a>.  Digging in, I eventually found a file called sys.php in the root of the site, and it listed the contents of my site and had a form that allowed someone to add a page, chmod a page, or delete a page.  Killer!</p>
<p>I found that it was defaulting to <em>/path/to/WP/wp-includes/plugins/wp-lytebo</em>x, and sure enough, digging into that directory revealed several other fun scripts, all of which gave someone the ability to access all the files on my site.  Fun!</p>
<p>I found that I already had replaced this plugin before, so I decided to get rid of it altogether, this now proving it wasn&#8217;t a misconfiguration, but rather, <a href="http://www.f-secure.com/vulnerabilities/SA200902421">a problem with the wp-lytebox itself</a>.</p>
<p>In this process, however, I was unable to fix my issue.  Visiting <em>/wp-admin/post-new.php</em> still rendered only a page footer, and nothing more.</p>
<p>So I starting fooling around in my directories looking for files that had been modified more recently than when I did my 2.9.2 upgrade.  One of the files? My <em>.htaccess</em> file.</p>
<p>&#8220;<em>This be odd,</em>&#8221; I thought to myself, &#8220;<em>I&#8217;ve changed this not, methinks.</em>&#8221;</p>
<p>Sure enough, there was a rogue line within: RewriteCond ^/default/$ /wp-admin/includes Huh?</p>
<p>I dug into that folder, and the .htaccess file there was recent too? It&#8217;s contents? <em>DefaultIndex users.php</em></p>
<p>Of course, I immediately opened users.php and found, as you might have guessed, a bunch of Russian crap. Savvy WP hackers will know, it&#8217;s not a real file, there is no users.php in the real wp-admin/includes directory.</p>
<p>I also found a folder that had two large files, both named core.XXXX where XXXX was a 4 digit number, and a massive 40 MB error_log.  Yikes.</p>
<p>I thought I had everything cleaned out, and I truly believed that the way in was wp-lytebox.  Then <a href="http://wordpress.org/support/topic/379659">I found this</a>.  And sure enough, all of the listed files were compromised.  So I nuked all the files, and replaced them all.  D&#8217;oh!</p>
<p>So, if you&#8217;re arriving via Google or Bing or Yahoo!, do NOT use wp-lytebox.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.firsttube.com/read/ive-been-hacked-wp-lytebox-sucks/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Behind the Scenes at OSNews</title>
		<link>http://www.firsttube.com/read/behind-the-scenes-at-osnews/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/behind-the-scenes-at-osnews/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.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://www.firsttube.com/read/flip-301/</link>
		<comments>http://www.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, &#8230; <a href="http://www.firsttube.com/read/flip-301/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.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://www.firsttube.com/read/microsofts-web-app-gallery-fail/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/microsofts-web-app-gallery-fail/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/microsofts-web-app-gallery-fail/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Posting Your Latest Tweet in WordPress</title>
		<link>http://www.firsttube.com/read/posting-your-latest-tweet-in-wordpress/</link>
		<comments>http://www.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, &#8230; <a href="http://www.firsttube.com/read/posting-your-latest-tweet-in-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/posting-your-latest-tweet-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Add Latest Tweet to WordPress (Without a Plugin)</title>
		<link>http://www.firsttube.com/read/how-to-add-latest-tweet-to-wordpress/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/how-to-add-latest-tweet-to-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/how-to-add-latest-tweet-to-wordpress/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Sorting a Multi-Dimensional Array with PHP</title>
		<link>http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/</link>
		<comments>http://www.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; &#8230; <a href="http://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/sorting-a-multi-dimensional-array-with-php/feed/</wfw:commentRss>
		<slash:comments>76</slash:comments>
		</item>
		<item>
		<title>firsttube.com Upgraded To WordPress 2.7</title>
		<link>http://www.firsttube.com/read/firsttubecom-upgraded-to-wordpress-27/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/firsttubecom-upgraded-to-wordpress-27/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.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://www.firsttube.com/read/wordpress-27-rc1/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/wordpress-27-rc1/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/wordpress-27-rc1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Weirdness</title>
		<link>http://www.firsttube.com/read/php-weirdness/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/php-weirdness/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/php-weirdness/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OSNews vs. WordPress</title>
		<link>http://www.firsttube.com/read/osnews-vs-wordpress/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/osnews-vs-wordpress/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/osnews-vs-wordpress/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>An Argument for PHP</title>
		<link>http://www.firsttube.com/read/An-Argument-for-PHP/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/An-Argument-for-PHP/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/An-Argument-for-PHP/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>HAXX0RED</title>
		<link>http://www.firsttube.com/read/HAXX0RED/</link>
		<comments>http://www.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. &#8230; <a href="http://www.firsttube.com/read/HAXX0RED/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.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://www.firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/</link>
		<comments>http://www.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 &#8230; <a href="http://www.firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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://www.firsttube.com/read/how-to-really-survive-digg-on-a-shared-host/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

