<?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>Uncertain Binary Thoughts &#187; tinyurl</title>
	<atom:link href="http://ankurs.com/tag/tinyurl/feed/" rel="self" type="application/rss+xml" />
	<link>http://ankurs.com</link>
	<description>01110100110101010101011101100010</description>
	<lastBuildDate>Mon, 30 Aug 2010 16:51:02 +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>Creating your own service like tinyurl</title>
		<link>http://ankurs.com/2009/05/creating-your-own-service-like-tinyurl/</link>
		<comments>http://ankurs.com/2009/05/creating-your-own-service-like-tinyurl/#comments</comments>
		<pubDate>Sun, 24 May 2009 09:45:56 +0000</pubDate>
		<dc:creator>Ankur Shrivastava</dc:creator>
				<category><![CDATA[Stuff]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[tinyurl]]></category>

		<guid isPermaLink="false">http://ankurs.com/?p=505</guid>
		<description><![CDATA[Few weeks ago i came across this article on Techcrunch which says tinyurl is worth $46Million (of course before twitter started using bit.ly as default) which made me think how hard is it to make a simple service like tinyurl and i figured out its not hard. so i used apache&#8217;s mod_rewrite to rewrite the]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago i came across <a href="http://www.techcrunch.com/2009/03/30/if-bitly-is-worth-8-million-tinyurl-is-worth-at-least-46-million/">this</a> article on Techcrunch which says tinyurl is worth $46Million (of course before twitter started using bit.ly as default) which made me think how hard is it to make a simple service like tinyurl and i figured out its not hard. so i used apache&#8217;s mod_rewrite to rewrite the url and forward the code to a php script ( url.php ) which will then search for the url in database and take the use to the original url and for shorting, url it is inserted into a table with a auto increment primary key and the key is converted to base36 which contains [a-z] and [0-9], making the short code&#8230;</p>
<p>First create a database say &#8216;shorturl&#8217;<code><br />
CREATE TABLE IF NOT EXISTS `shorturl` (<br />
`id` int(11) NOT NULL AUTO_INCREMENT,<br />
`url` varchar(300) NOT NULL,<br />
PRIMARY KEY (`id`)<br />
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;<br />
</code><br />
then create file .htaccess where you want your url to be and write<code><br />
RewriteEngine On<br />
RewriteCond %{REQUEST_URI} \/([0-9a-z]*)$ [NC]<br />
RewriteRule ^(.*) url.php?url=%1 [L]<br />
</code></p>
<p>and to create the url we will use a simple file say create.php<br />
<code><br />
&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Url Shortner&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php<br />
ob_start();<br />
$host=""; //host<br />
$dbuser=""; // database username<br />
$dbpass=""; // database password<br />
$db=""; //database name<br />
$con = mysql_connect($host,$dbuser,$dbpass);<br />
mysql_select_db("url",$con);<br />
if (!isset($_POST['shorten']))<br />
{<br />
echo "&lt;center&gt;&lt;h3&gt;Enter the url to shorten&lt;/h3&gt;&lt;form method='POST'&gt;&lt;input type='text' name='url' /&gt;<br />
&lt;input type='submit' name='shorten' value='Shorten' /&gt;&lt;/form&gt;&lt;/center&gt;";<br />
}<br />
else<br />
{<br />
if (isset($_POST['url']))<br />
{<br />
$url=" ".mysql_real_escape_string($_POST['url']);<br />
if (strpos($url,"http://") or strpos($url,"https://") or strpos($url,"ftp://"))<br />
{<br />
$url=mysql_real_escape_string($_POST['url']);<br />
}<br />
else<br />
{<br />
$url="http://".mysql_real_escape_string($_POST['url']);<br />
}<br />
$q="select * from url where url='{$url}'";<br />
$res = mysql_query($q,$con);<br />
$row=mysql_fetch_row($res);<br />
if (!$row)<br />
{<br />
$query="insert into url set url='{$url}'";<br />
$res = mysql_query($query,$con);<br />
}<br />
$query1="select id from url where url='{$url}' limit 1";<br />
$res=mysql_query($query1,$con);<br />
$row=mysql_fetch_row($res);<br />
echo "shortened url is http://yoursite.com/".base_convert($row[0],10,36);<br />
}<br />
}<br />
ob_end_flush();<br />
?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
</code><br />
then our url.php file<br />
<code><br />
&lt;?php<br />
ob_start();<br />
$host=""; //host<br />
$dbuser=""; // database username<br />
$dbpass=""; // database password<br />
$db=""; //database name<br />
$con = mysql_connect($host,$dbuser,$dbpass);<br />
mysql_select_db($db,$con);<br />
if (isset($_GET['url']))<br />
{<br />
$query="select * from url where id ='".base_convert(mysql_real_escape_string($_GET['url']),36,10)."'";<br />
$res = mysql_query($query,$con);<br />
$row=mysql_fetch_row($res);<br />
header("Location: ".$row[1]);<br />
}<br />
ob_end_flush();<br />
?&gt;</code></p>
<p>Download <a href="http://ankurs.com/wp-content/uploads/2009/05/urltar.gz">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ankurs.com/2009/05/creating-your-own-service-like-tinyurl/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
