I hope you all got a clear picture in the first part. We only covered the CSS and Front End part, now we need to create actions and validations page to shorten the link successfully.
For validating and showing results we will be using Mootools (a AJAX framework) i will not be explaining in detail on mootools right now.
Here is the actions.php file:
//To post values from index.html $url=$_POST['originalurl']; $alias=$_POST['alias']; $count=0;
e are initially making count as 0 becomes this is being created for the first time.
Assigning orginalurl and alias to $url and $alias respectively.
Now we need to validate the URL as well as ALIAS.
Should Validate:
1. URL must be a URL and not just a TEXT
2. If alias field is blank then it must randomly create some characters.
3. If alias field has some value but its less than 3 characters then an error must be thrown.
4. Alias name shouldn’t contain any special characters.
4. If alias name is already being used by someone then again an error must be thrown.
So here is the code for that i will be using mootools so that i can display the result at the same page.
if(validate_url($url)==FALSE)
{
$result[] = 'Please enter only URL which start with http:// we wont allow you to tiny SHIT';
}
if($alias=='')
{
$alias=chr(rand(97,122)).chr(rand(97,122)).chr(rand(97,122));
}
if(strlen($alias)<3)
{
$result[] = 'Alias name must be greater than 3 chracters';
}
if(alpha_numeric($alias)==FALSE)
{
$result[] = 'No Special Characters must be used.';
}
if(alias_check($alias)==FALSE)
{
$result[] = 'Alias Name is already being used be someone, please specify another alias name';
}
In the above code we are using few functions (functin.php) like validate_url, alpha_numeric, alias_check and these functions are in function.php
unction alpha_numeric($alias)
{
return ( ! preg_match("/^([-a-z0-9])+$/i", $alias)) ? FALSE : TRUE;
}
function validate_url($url)
{
return (!preg_match("/http|ftp:\/\/(.*)\.(.*)/i", $url)) ? FALSE : TRUE;
}
function alias_check($alias)
{
include('config.php');
$sql="SELECT alias FROM url_tables WHERE alias='".$alias."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
if( mysql_num_rows($result) == 0 )
{
return(TRUE);
}
else{
return(FALSE);
}
}
The $sql is used for checking the presence of alias name.
Now we completed the validation part. If everything goes well then we have to insert to our database.
Here is the code for that:
$query1 = mysql_query("INSERT INTO url_tables (alias,url,count) VALUES ('$alias','$url','$count')");
The mootools function that is being added on index.html is here:
Finally we completed 90% of our code. Its time for retrieving original URL from alias URL and also increment the count to show that the alias name was used.
We call this as original.php, in the page we grab the alias name check in out database and if found we pull the original URL else give an error showing that it is an invalid alias name.
$Not_Found = "reminder.html"; // The page to redirect to if no redirect link is found in the database.
$get_alias = $_SERVER['QUERY_STRING'];
$sql = mysql_query("SELECT url FROM url_tables WHERE alias='$get_alias'");
$site_arr = mysql_fetch_array($sql);
$site_redirect = $site_arr['url'];
if(!$site_arr){ header("Location: $Not_Found"); }
if($site_arr){
mysql_query("UPDATE url_tables SET ount = count + 1 WHERE alias='$get_alias'");
header("Location: $site_redirect");
}
$Not_Found is used to display a page if the alias name is invalid. And the remaining part is to query using Alias name as the ID and jump to the original URL. We also increment count = count + 1
Now making the magic of URL. Actually the above code works like this: http://gotiny.co.cc/original.php?example but we have to change it to http://gotiny.co.cc/example this is done by creating a .htaccess file
Here is the code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ original.php?$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ original.php?$1
ErrorDocument 404 /errror404.htm
More tricks on .htaccess is here
At last we completed a bit.ly kind of site. For the demo site click here. Full full source download here.
If you have not read the Part 1 of this tutorial click here.
Waiting for your comments and thanks.
Happy Coding
CHOW
the process is quite long…need to read twice b4 performing this..:-)
Wow
Where can I see this “live” (in action)?
Thanks
the Gotiny.co.cc website itself is the live action of the code..read the post carefully…!!
Nice tutorial i was actually using bit.ly api now i can use this instead of depending on bit.ly
Nice work Ragz , Neat and clean always . It really helps people who are more dependent on API’s . New Chut.ney instead of Bit.ly
nice post. thanks.
Good Day!!! http://www.talkonsomething.com is one of the most outstanding resourceful websites of its kind. I take advantage of reading it every day. Keep it that way.
Thanks for the appreciation
Do Subscribe to our Email Updates & SMS Updates.