Creating your own service like tinyurl

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’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…

First create a database say ’shorturl’
CREATE TABLE IF NOT EXISTS `shorturl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(300) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

then create file .htaccess where you want your url to be and write
RewriteEngine On
RewriteCond %{REQUEST_URI} \/([0-9a-z]*)$ [NC]
RewriteRule ^(.*) url.php?url=%1 [L]

and to create the url we will use a simple file say create.php

<html>
<head>
<title>Url Shortner</title>
</head>
<body>
<?php
ob_start();
$host=""; //host
$dbuser=""; // database username
$dbpass=""; // database password
$db=""; //database name
$con = mysql_connect($host,$dbuser,$dbpass);
mysql_select_db("url",$con);
if (!isset($_POST['shorten']))
{
echo "<center><h3>Enter the url to shorten</h3><form method='POST'><input type='text' name='url' />
<input type='submit' name='shorten' value='Shorten' /></form></center>";
}
else
{
if (isset($_POST['url']))
{
$url=" ".mysql_real_escape_string($_POST['url']);
if (strpos($url,"http://") or strpos($url,"https://") or strpos($url,"ftp://"))
{
$url=mysql_real_escape_string($_POST['url']);
}
else
{
$url="http://".mysql_real_escape_string($_POST['url']);
}
$q="select * from url where url='{$url}'";
$res = mysql_query($q,$con);
$row=mysql_fetch_row($res);
if (!$row)
{
$query="insert into url set url='{$url}'";
$res = mysql_query($query,$con);
}
$query1="select id from url where url='{$url}' limit 1";
$res=mysql_query($query1,$con);
$row=mysql_fetch_row($res);
echo "shortened url is http://yoursite.com/".base_convert($row[0],10,36);
}
}
ob_end_flush();
?>
</body>
</html>

then our url.php file

<?php
ob_start();
$host=""; //host
$dbuser=""; // database username
$dbpass=""; // database password
$db=""; //database name
$con = mysql_connect($host,$dbuser,$dbpass);
mysql_select_db($db,$con);
if (isset($_GET['url']))
{
$query="select * from url where id ='".base_convert(mysql_real_escape_string($_GET['url']),36,10)."'";
$res = mysql_query($query,$con);
$row=mysql_fetch_row($res);
header("Location: ".$row[1]);
}
ob_end_flush();
?>

Download here

Tags: , ,

This entry was posted on Sunday, May 24th, 2009 at 3:15 pm and is filed under Stuff, fedora. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “Creating your own service like tinyurl”

anomit May 24th, 2009 at 5:16 pm

Haha, nice way to show them :D

Surjodeb Basu May 24th, 2009 at 10:53 pm

You forgot the most important thing that makes these sites as successful as they are.- The tiny domain url.

What you need first & foremost is a really Small & yet Rememberable domain url.something catchy.

And i am sure that wont come cheap. =)

AnkurG May 25th, 2009 at 12:04 am

great, what all required to understand this coding is learning ‘php’ for me. :(

Kevin Kofler May 25th, 2009 at 4:53 am

There’s also http://urlx.eu/ whose source code is published.

BobMarche June 11th, 2009 at 10:28 am

Thanks for the useful info. It’s so interesting

Leave a Reply