Ever wanted to start your own tiny url website ? This post is for the people who wanted to start there own tiny url or shorten url service with minimum technical knowledge.
In this example we have taken a example of http://tinurl.co
What is required ?
You need a domain name (really short) and a linux shared hosting service a basic knowledge of PHP, Mysql and Apache.
The web site structure
For creating a tiny url like website you need five files in total :
1: Index.php :
This is your main page, which ask user for the main url or long url to shorten the same.
2: random.php :
This file will create a shorten url for the entered long url.
3: redirect.php :
This is a standalone file which will actually do the redirection to long url when user refer the shorten url.
4: .htaccess
.htaccess file tell the apche server how to treat some special request like 404 error or any redirection and much more. In this case it will tell the apache server about the redirection method to use for shorten url.
5: db.php
Over here we will store the database connection for mysql.
The Mysql Structure :
This is very simple database structure i ever used. There is only one table here which i named as ” link ” because it will store all the requested long url and there shorter form.
The Table structure will have three fields :
1: sno
A auto increment field int value, this will your primary key.
2. actualurl
This field with VARCHAR type and limit of 255 will store the actual long url entered by the user.
3. tiny
As the name implement it will store the shorten form of entered long url.
Ok, now lots of theory is done here lets work on the coding part, I will go page wise :
db.php
<?php
$my_host = 'localhost'; // your host here
$my_user = 'db_username'; // your database url
$my_pass = 'db_password'; // your database password
$my_database = 'db_name'; // your database name
// database connection string
$conn = mysql_connect($my_host, $my_user, $my_pass) or die ('Error connecting to mysql');
@mysql_select_db("my_$database") or die( "Unable to select database");
?>
index.php
<?php
include_once('db.php'); // making the db connection
?>
<!-- java script to verify the user input like it should be with http:// -->
<SCRIPT LANGUAGE="JavaScript">
function Validate(form) {
var v = new RegExp();
v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
if (!v.test(form["textfield"].value)) {
alert("You must supply a valid URL.");
return false;
}
}
</SCRIPT>
<body topmargin="0" leftmargin="0">
<table width="100%" border="0">
<tr>
<td height="64" colspan="2" bgcolor="#003333"><h1 class="style2">URL shortener tools</h1></td>
</tr>
<tr>
<td width="7%"> </td>
<td width="93%">
<?php
if($_POST[textfield] != '')
{
include_once('random.php');
$query = "select * from links where actualurl ='$_POST[textfield]' limit 1";
$result = mysql_query($query) or die(mysql_query());
$count1 = mysql_num_rows($result);
if($count1 =='0')
{
mysql_query("insert into links (actualurl,tiny) values ('$_POST[textfield]','$pwd')") or die(mysql_error());
}else
{
while($row = mysql_fetch_array($result))
{
$pwd = $row[tiny];
}
}
?>
<table width="60%" border="1" align="center">
<tr>
<td>
http://tinurl.co/<?php echo $pwd; ?> <a href="http://tinurl.co/<?php echo $pwd; ?>" target="_blank"><img src="images/bullet1.gif" width="8" height="9" border="0" /> </a> </td>
</tr>
</table>
<br />
<?php
}
?>
<form id="form1" name="form1" method="post" action="" onSubmit="return Validate(this);">
<table width="75%" border="0" align="center">
<tr bgcolor="#CCCCCC">
<td colspan="2"><span class="style1">Enter Your URL Bellow :</span></td>
</tr>
<tr>
<td width="24%">Your Big URL with http://</td>
<td width="76%"><label>
<input name="textfield" type="text" id="textfield" size="50" />
<input type="submit" name="button" id="button" value="Submit" />
</label></td>
</tr>
<tr>
<td colspan="2"> Custom Link http://tinurl.co/
<label>
<input type="text" name="c" id="c" />
(Optional)</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
3: random.php
<?php
$min=3; // minimum length of url
$max=4; // maximum length of url
$pwd=""; // to store generated url
for($i=0;$i<rand($min,$max);$i++)
{
// to see if the entered url is already present in db
$query = "select * from links where tiny ='$pwd'";
$result = mysql_query($query) or die(mysql_error());
$count_db = mysql_num_rows($result);
if($count_db =='0')
{
$num=rand(48,122);
if(($num > 97 && $num < 122))
{
$pwd.=chr($num);
}
else if(($num > 65 && $num < 90))
{
$pwd.=chr($num);
}
else if(($num >48 && $num < 57))
{
$pwd.=chr($num);
}
else if($num==95)
{
$pwd.=chr($num);
}
else
{
$i--;
}
}
}
//echo $pwd; // prints the password
?>4. redirect.php
<?php
require("./db.php");
$short = $_REQUEST['short'];
$query = mysql_query("SELECT * FROM links WHERE `tiny`='".mysql_escape_string($short)."' LIMIT 1");
$row = mysql_fetch_row($query);
if(!empty($row)) {
Header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$row[1]."");
} else {
$html = "Error: cannot find short URL";
}
mysql_close($db);
?>5: .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.tinurl.co$
RewriteRule ^/?$ "http\:\/\/tinurl\.co" [R=301,L]
RewriteRule ^(\w+)$ ./redirect.php?short=$1I hope that you enjoyed reading this post and you will be now able to start your own tiny url website. Enjoy ….
To get your domain name registered and linux hosting your can visit NetSpaceIndia.com




