<?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>Sean-O.com &#187; Tips</title>
	<atom:link href="http://www.sean-o.com/blog/index.php/category/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sean-o.com/blog</link>
	<description>Digital Craftsman &#38; Curator</description>
	<lastBuildDate>Thu, 26 May 2011 17:30:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Tutorial: How To Create Your Own URL Shortener</title>
		<link>http://www.sean-o.com/blog/index.php/2009/08/11/tutorial-how-to-create-your-own-url-shortener/</link>
		<comments>http://www.sean-o.com/blog/index.php/2009/08/11/tutorial-how-to-create-your-own-url-shortener/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 19:15:15 +0000</pubDate>
		<dc:creator>seano</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[links]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[short URL]]></category>
		<category><![CDATA[shortener]]></category>
		<category><![CDATA[shortURL]]></category>
		<category><![CDATA[tr.im]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://www.sean-o.com/blog/?p=951</guid>
		<description><![CDATA[I&#8217;ve been thinking about posting this for a while now. Then came the announcement on Sunday, from URL shortening service tr.im, that they are shutting down, effective immediately. It brought to light the issues with relying on the cloud. Admittedly, this is not as devastating an announcement as your trusted e-mail, or even photo hosting, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been thinking about posting this for a while now. Then came the announcement on Sunday, from URL shortening service <strong>tr.im</strong>, that <a title="Tr.im URL Shortening Service Announces Shutdown" href="http://blog.tr.im/" target="_blank">they are shutting down</a>, effective immediately. It brought to light the issues with relying on the cloud.</p>
<p>Admittedly, this is not as devastating an announcement as your trusted e-mail, or even photo hosting, service going under. However, social media sites &#8212; primarily Twitter &#8212; have made URL shorteners almost a necessity, and some users have developed quite a bit of &#8220;social capital&#8221; in the form of links built and shared using a particular service.</p>
<p>Although tr.im promises to keep existing links active through the end of the year, there is currently no way to create new short URLs, nor is there any way to view information or statistics on any existing tr.im links. Poof, they&#8217;re gone. No warning, no recourse.</p>
<p><strong>UPDATE</strong>: Tr.im has restored their service. However, this unpredictability only reinforces the call to control your site and your brand.</p>
<p>Okay, so what can you do about it?<br />
<strong><em>Make your own URL shortener.</em></strong></p>
<p>As you&#8217;ll soon see, it&#8217;s easy, it&#8217;s fun, and puts the <strong>you</strong> in short URLs.</p>
<h3>Requirements</h3>
<ul>
<li>Web Server &#8212; preferably one you host yourself, with control over basic site settings</li>
<li>Web Development Language &#8212; using PHP here, but this code can be adapted to ASP.NET, ColdFusion, etc.</li>
<li>Database &#8212; MySQL featured, but SQL Server, PostgreSQL, etc. would work</li>
<li>Ideally, you&#8217;ll want a short domain to set this up on.<br />www.joesautoglassandspareparts.com kinda defeats the purpose ;)</li>
</ul>
<h3>Examples</h3>
<p>Each of the following are short URLs to content on other sites:<br />
<a href="http://sean-o.com/babysafe" target="_blank">http://sean-o.com/babysafe</a><br/><br />
<a href="http://sean-o.com/getwindows7" target="_blank">http://sean-o.com/getwindows7 </a><br/><br />
<a href="http://sean-o.com/playtime" target="_blank">http://sean-o.com/playtime</a><br/></p>
<h3>Getting Started</h3>
<p>As mentioned above, we&#8217;ll be using PHP 5.2.x &amp; MySQL 4/5 for this tutorial &#8212; as these open-source technologies should be available to most of you. </p>
<p>The first thing you&#8217;ll need is a source for your short URLs. Easiest way to do that is with a MySQL database (or new table on your existing site&#8217;s database). Here&#8217;s the structure I used:</p>
<div class="wp-caption alignnone" style="width: 519px"><img title="Sean O's Short URL Tutorial: Table Structure" src="http://sean-o.com/images/shorturl_table.gif" alt="Sean O's Short URL Tutorial: Table Structure" width="509" height="158" /><p class="wp-caption-text">Sean O's Short URL Tutorial: Table Structure</p></div>
<p>You only really need the first three columns, the rest are for statistics &amp; future use (userID). Add/remove fields as you see fit &#8212; perhaps you may want a field for notes?</p>
<h3>The Script</h3>
<p>We&#8217;ll only need one script to accomplish the short URL resolution. Create a file called <em>shortURL.php</em> at the root of your site. (or you may choose a custom name &#038; path)</p>
<p>To begin, the first thing to do is grab the short URL &#8212; the segment after the base URL.<br />
e.g. http://sean-o.com/<strong>playtime</strong><br />
A little regex (regular expressions) goes a long way here to parse the short URL and strip extraneous characters.</p>
<pre lang="php">$expectedURL = trim($_SERVER['URL']);
$split = preg_split("{:80\/}",$expectedURL);
$shortURL = $split[1];
// security: strip all but alphanumerics &#038; dashes
$shortURL = preg_replace("/[^a-z0-9-]+/i", "", $shortURL);
</pre>
<p>Next, we&#8217;ll check this string to see if it matches a short URL in our database.</p>
<pre lang="php">$isShortURL = false;
$result = getLongURL($shortURL);
if ($result) { $isShortURL = true; }
$longURL = $result['longURL'];</pre>
<p>Finally, we check to see if our <em>$isShortURL</em> flag is set. If a matching short URL was found, we&#8217;ll redirect to it. If not, we&#8217;ll display our standard 404.</p>
<pre lang="php">if ($isShortURL)
{
	redirectTo($longURL, $shortURL);
} else {
	show404();  // no shortURL found, display standard 404 page
}</pre>
<h3>The Functions</h3>
<p>The primary function &#8212; get the long URL associated with the passed short URL, if it exists.</p>
<pre lang="php">function getLongURL($s)
{
	// define these variables for your system
	$host = ""; $user = ""; $pass = ""; $db = "";
	$mysqli = new mysqli($host, $user, $pass, $db);
	// you may just want to fall thru to 404 here if connection error
	if (mysqli_connect_errno()) { die("Unable to connect !"); }
	$query = "SELECT * FROM urls WHERE shorturl = '$s';";
	if ($result = $mysqli->query($query)) {
	    if ($result->num_rows > 0) {
		while($row = $result->fetch_assoc()) {
			return($row);
		}
	    } else {
	    	return false;
	    }
	} else {
		return false;
	}
	$mysqli->close();
}</pre>
<p>Perform the URL redirection.</p>
<pre lang="php">function redirectTo($longURL)
{
	// change this to your domain
	header("Referer: http://www.your-domain-here.com");
	// use a 301 redirect to your destination
	header("Location: $longURL", TRUE, 301);
	exit;
}</pre>
<p>Finally, display your standard 404 page here.</p>
<pre lang="php">function show404()
{
	// display/include your standard 404 page here
	echo "<html><head></head><body>404 Page Not Found.</body></html>";
	exit;
}</pre>
<h3>Wire It Up</h3>
<p>Okay, so we have the script&#8230; now what? Well, the &#8220;special sauce&#8221; here is the <strong>404 redirect</strong>. What we&#8217;re simply doing is replacing (or augmenting) your site&#8217;s 404 page with one that checks a database for a URL shortcut. If one is listed that matches, redirect to it. If not, display your standard (or <a href="http://www.instantshift.com/2009/03/24/creative-404-error-pages-around-for-inspiration/" target="_blank">not so standard</a>) 404 error message.</p>
<p>You&#8217;ll need to modify your site&#8217;s existing 404 error page, or (recommended) create a new one. If creating a new one, make sure your site is set to point to this file. For IIS (5/6): Go to your web site, <em>Properties, Custom Errors, 404, Edit Properties&#8230;</em> For Apache, edit your .htaccess file thusly: <em>ErrorDocument 404 /shortURL.php</em>. (replace with your custom path as appropriate)</p>
<h3>Now, Run With It (Additional, Optional Steps)</h3>
<p>If you&#8217;ll be using this with any regularity, you&#8217;ll probably want to create an admin panel page to quickly add &amp; manage your URLs (I&#8217;m simply using a MySQL GUI &#8212; the great <a title="HeidiSQL - MySQL GUI" href="http://www.heidisql.com" target="_blank">HeidiSQL</a>). Consider whether you want to use custom short URL names, or just generate a random 4-5 character string (or both!). If you&#8217;re a statistics nut, you might want to capture more than just the user&#8217;s IP and/or build a stats page.</p>
<p>If you&#8217;re looking to &#8220;monetize&#8221; (ahem, see above), you may want to frame the linked site under a toolbar with your site&#8217;s branding, a la the &#8220;Digg Bar&#8221;. I highly recommend against this, however, as many users consider this practice an annoyance.</p>
<p>If your domain name is longer than, say, 8 characters, consider purchasing a second domain for exclusive short URL serving. Two-letter country-based domains such as .ly and .to are popular. Flickr&#8217;s recent purchase of http://flic.kr was especially clever.</p>
<h3>Conclusion</h3>
<p>So there you have it. Your very own, shiny new, URL shortening service&#8230; in <strong>under 50 lines</strong> of code!</p>
<p>Be sure to test. I suggest trying the following: a short URL you&#8217;ve created, a known working URL on your site (make sure you didn&#8217;t break anything!), and a known incorrect URL (test the standard 404). Also, be mindful of the security implications. Make sure following best practices with regards to MySQL security, and be sure to sanitize all URLs. Ensure your server is running the latest versions of software with the latest patches.</p>
<p><strong>Disclaimer</strong>: This article is just to get you started. The code presented here is most certainly &#8220;quick &amp; dirty&#8221;, and can surely be optimized. This code may or may not work for you, and I cannot be held responsible for any damage that may occur to your site as a result of implementing this.</p>
<p>If you end up implementing this on your site, I&#8217;d love to see it. Post an example here in the comments, or <strong><a href="http://twitter.com/seanodotcom" target="_blank">follow me on twitter</a></strong> and hit up @seanodotcom. If you&#8217;ve ported this code to other languages/databases, drop me a line.</p>
<p>Thanks for reading,<br />
<strong><em>SEAN O</em></strong><br />
<a href="http://twitter.com/seanodotcom"><img title="Follow Sean O (@seanodotcom) on Twitter" src="http://sean-o.com/images/icons/twitter/twitter-03.png" alt="Follow Sean O (@seanodotcom) on Twitter" width="80" height="15" /></a><br />
<br clear='all' /></p>
<h3>Advantages</h3>
<ul>
<li>You&#8217;re in complete control</li>
<li>You can specify random short URLs or custom ones, using related keyword(s)</li>
<li>You can collect virtually any statistics you wish</li>
<li>Excellent for site branding &#8212; your URL appears on every link!</li>
</ul>
<h3>Disadvantages</h3>
<ul>
<li>You&#8217;re responsible for maintaining uptime &#8212; as your site goes, so goes the service</li>
<li>You need to build any related services &#8212; stats tracking, APIs, user registration, etc.</li>
<li>Your site&#8217;s subdirectory names will override custom short URLs of the same name</li>
<li>Every 404 on your site will incur a database hit &#8212; be sure to keep your main site links fresh (<i>Thanks 5Min</i>)
<li>Utility varies inversely with the length of the primary domain name</li>
</ul>
<h3>Alternatives</h3>
<ul>
<li>Simply use another service &#8212; bit.ly, cli.gs, etc. (but that&#8217;s no fun, and you&#8217;re again vulnerable)</li>
<li>Creative use of <em>htaccess</em> or <em>IIS Rewrite</em></li>
</ul>
<p></p>
<p><em>p.s. the short URL for this post is <a href="http://sean-o.com/short-URL" target="_blank">http://sean-o.com/short-URL</a></p>
<p><a class="retweet" href="http://sean-o.com/short-URL" target="_blank">http://sean-o.com/short-URL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-o.com/blog/index.php/2009/08/11/tutorial-how-to-create-your-own-url-shortener/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>Revaluing Time &amp; Attention</title>
		<link>http://www.sean-o.com/blog/index.php/2008/08/14/revaluing-time-attention/</link>
		<comments>http://www.sean-o.com/blog/index.php/2008/08/14/revaluing-time-attention/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 20:58:45 +0000</pubDate>
		<dc:creator>seano</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Culture]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[attention]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[GTD]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[management]]></category>
		<category><![CDATA[merlin]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.sean-o.com/blog/?p=809</guid>
		<description><![CDATA[Merlin Mann, a smart &#38; funny guy who has some helpful advice about managing Time &#38; Attention for the fellow Knowledge Workers among us.  The slides are excerpted from a 45-minute presentation, distilled to the major points. Who Moved My Brain? Revaluing Time &#38; Attention View SlideShare presentation or Upload your own. (tags: creativity productivity)]]></description>
			<content:encoded><![CDATA[<p>Merlin Mann, a smart &amp; funny guy who has some helpful advice about managing Time &amp; Attention for the fellow Knowledge Workers among us.  The slides are excerpted from a 45-minute presentation, distilled to the major points.</p>
<div style="width:425px;text-align:left" id="__ss_554937"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/merlinmann/who-moved-my-brain-revaluing-time-attention-presentation?src=embed" title="Who Moved My Brain? Revaluing Time &amp; Attention">Who Moved My Brain? Revaluing Time &amp; Attention</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=mann-who-moved-my-brain-time-attention-20080814-1218719955598003-8&#038;stripped_title=who-moved-my-brain-revaluing-time-attention-presentation" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc=mann-who-moved-my-brain-time-attention-20080814-1218719955598003-8&#038;stripped_title=who-moved-my-brain-revaluing-time-attention-presentation" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View SlideShare <a style="text-decoration:underline;" href="http://www.slideshare.net/merlinmann/who-moved-my-brain-revaluing-time-attention-presentation?src=embed" title="View Who Moved My Brain? Revaluing Time &amp; Attention on SlideShare">presentation</a> or <a style="text-decoration:underline;" href="http://www.slideshare.net/upload?src=embed">Upload</a> your own. (tags: <a style="text-decoration:underline;" href="http://slideshare.net/tag/creativity">creativity</a> <a style="text-decoration:underline;" href="http://slideshare.net/tag/productivity">productivity</a>)</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-o.com/blog/index.php/2008/08/14/revaluing-time-attention/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>You&#8217;ll Lose Sleep Over This</title>
		<link>http://www.sean-o.com/blog/index.php/2008/03/08/youll-lose-sleep-over-this/</link>
		<comments>http://www.sean-o.com/blog/index.php/2008/03/08/youll-lose-sleep-over-this/#comments</comments>
		<pubDate>Sun, 09 Mar 2008 00:56:15 +0000</pubDate>
		<dc:creator>seano</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[clocks]]></category>
		<category><![CDATA[daylight]]></category>
		<category><![CDATA[dst]]></category>
		<category><![CDATA[saving]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://www.sean-o.com/blog/index.php/2008/03/08/youll-lose-sleep-over-this/</guid>
		<description><![CDATA[Since Daylight Saving Time has been expanded, it is no longer the bellwether of Spring it once was &#8212; and certainly doesn&#8217;t feel like it here on the East Coast.  But, in any event, it happens tonight, and (seemingly) longer days are ahead. Spring ahead, suckers!]]></description>
			<content:encoded><![CDATA[<p><img src="http://media.klewtv.com/images/080307_spring_forward.jpg" alt="Daylight Savings" border="0" height="240" hspace="8" vspace="8" width="320" />Since <a href="http://en.wikipedia.org/wiki/Daylight_saving_time" title="Daylight Saving Time" target="_blank">Daylight Saving Time</a> has been expanded, it is no longer the bellwether of Spring it once was &#8212; and certainly doesn&#8217;t feel like it here on the East Coast.  But, in any event, it happens tonight, and (seemingly) longer days are ahead.</p>
<h3>Spring ahead, suckers!</h3>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-o.com/blog/index.php/2008/03/08/youll-lose-sleep-over-this/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image Stabilize Your Camera&#8230; For A Buck</title>
		<link>http://www.sean-o.com/blog/index.php/2008/01/30/image-stabilize-your-camera-for-a-buck/</link>
		<comments>http://www.sean-o.com/blog/index.php/2008/01/30/image-stabilize-your-camera-for-a-buck/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 04:25:23 +0000</pubDate>
		<dc:creator>seano</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[blurry]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[lens]]></category>
		<category><![CDATA[Photos]]></category>
		<category><![CDATA[stabilize]]></category>

		<guid isPermaLink="false">http://www.sean-o.com/blog/index.php/2008/01/30/image-stabilize-your-camera-for-a-buck/</guid>
		<description><![CDATA[Clever trick using physics to keep your camera steady and put an end to blurry shots. $1 Image Stabilizer For Any Camera &#8211; Lose The Tripod]]></description>
			<content:encoded><![CDATA[<p>Clever trick using physics to keep your camera steady and put an end to blurry shots.<br />
<embed src="http://www.metacafe.com/fplayer/1041948/1_image_stabilizer_for_any_camera_lose_the_tripod.swf" width="400" height="345" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"> </embed><br /><font size = 1><a href="http://www.metacafe.com/watch/1041948/1_image_stabilizer_for_any_camera_lose_the_tripod/">$1 Image Stabilizer For Any Camera &#8211; Lose The Tripod</a></font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-o.com/blog/index.php/2008/01/30/image-stabilize-your-camera-for-a-buck/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Control Your Finances In 2008</title>
		<link>http://www.sean-o.com/blog/index.php/2008/01/08/control-your-finances-in-2008/</link>
		<comments>http://www.sean-o.com/blog/index.php/2008/01/08/control-your-finances-in-2008/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 04:55:18 +0000</pubDate>
		<dc:creator>seano</dc:creator>
				<category><![CDATA[Money]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[retirement]]></category>
		<category><![CDATA[savings]]></category>

		<guid isPermaLink="false">http://www.sean-o.com/blog/index.php/2008/01/08/control-your-finances-in-2008/</guid>
		<description><![CDATA[Get Rich Slowly offers a mostly-common-sense-but-still-informative article for making the most of your money in the new year. Of the tips offered, I would emphasize maxing out your Retirement Savings now. As in right now. As in a tomorrow morning trip to HR. The sooner you start saving, the exponential advantage you&#8217;ll have in creating [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.getrichslowly.org" title="Get Rich Slowly">Get Rich Slowly</a> offers a mostly-common-sense-but-still-informative article for <a href="http://www.getrichslowly.org/blog/2008/01/07/8-ways-to-take-control-of-your-finances-in-2008/" title="Take Control of your finances in 2008" target="_blank">making the most of your money in the new year</a>.</p>
<p>Of the tips offered, I would emphasize maxing out your Retirement Savings <strong>now</strong>.  As in right now.  As in a tomorrow morning trip to HR.  The sooner you start saving, the <em>exponential</em> advantage you&#8217;ll have in creating your (ideally at least) million dollar nest egg.  Don&#8217;t think you can just &#8220;save a little now and put away more later&#8221;, &#8217;cause brother, <a href="http://allthingsfinancialblog.com/2006/05/22/the-cost-of-waiting-one-year/" title="The Cost of waiting to save">you&#8217;re in for a shock</a>.  And if your company offers matching contributions &#8212; otherwise known as <strong>free G.D. money</strong> &#8212; say &#8220;Yes, please.&#8221;</p>
<p>Along similar lines,  the Automate Your Finances section says it best: &#8220;<em>When you make things automatic, you remove the human element, making it more difficult for you to mess things up.</em>&#8221;</p>
<p>If you&#8217;re able, keep enough money in your checking account to <strong>pay utilities automatically online</strong>.  Save the hassle, the time and the stamps (money).  Also, this helps you to <strong>avoid making late</strong> <strong>payments</strong>, which by default will help your credit.</p>
<p>And save automatically as well!  Just about every bank now has a web site to help you manage your money.  Set up automatic transfers to move $50, $100, or whatever amount you can afford into your Savings Account or (preferably) Money Market account every month.  <strong>Pay yourself first</strong>.  Save without thinking.  Enjoy <a href="http://www.mindyourfinances.com/money-management/savings/081104-04" title="The wonders of compound interest" target="_blank">the wonders of compound interest</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sean-o.com/blog/index.php/2008/01/08/control-your-finances-in-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

