create url shortener in php

create

If you are an admin of a website or a web programmer then You may want to use a URL shortner
Because by using a url shortner you can use short url to display instead of long one
The search engines may also dislike if there are so many outgoing link from your website
So by create url shortener in php you an solve issues like this

So let us create url shortener in php

For create url shortener in php we need to have a debase table for storing information

Let us create a database table

CREATE TABLE `url_table` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`furl` TEXT NOT NULL ,
`surl` VARCHAR( 255 ) NOT NULL ,
`click` INT( 11 ) NOT NULL
) ENGINE = MYISAM ;

The furl is the one which store all full site url
The surl store all shortened url

for create url shortener in php we need two php files
Let us say url shortner.php to shorten the url and u.php
For redirect to the full url website

Code for urlshortner.php

$hostname = 'localhost';
$dbname = 'labs';
$db_user = 'root';
$db_password = '';
$link = mysql_connect($hostname,$db_user,$db_password);
$connect = mysql_select_db($dbname,$link)or die(mysql_error());
$connect = mysql_select_db($dbname,$link)or die(mysql_error());

if($_POST['sh_url']){

while(1)
  {
$sec_id=generateRandomString(4);
$check_sec_id=mysql_num_rows(mysql_query("SELECT * from url_table WHERE surl='$sec_id'"));
 if(!$check_sec_id)
 break;
 }
$furl=$_POST['url'];
$sql=mysql_query("INSERT INTO `url_table` (`furl` ,`surl`)VALUES ('$furl','$sec_id')");
if($sql){
$server_add=$_SERVER['SERVER_NAME'];
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
$server_add="https://".$server_add ;
} else {
$server_add="http://".$server_add;
}
$shortnedurl=$server_add."/u.php?w=".$sec_id;
echo "<a href="$shortnedurl" target="_blank">$shortnedurl</a>";
}

}

function generateRandomString($length = 4) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; } 

This code will insert the full URL and shortened into the database table It will give a short link by clicking that link it will go to u.php From u.php it will redirect to the corresponding website Code for u.php

 $hostname = 'localhost'; $dbname = 'labs'; $db_user = 'root'; $db_password = ''; $link = mysql_connect($hostname,$db_user,$db_password); $connect = mysql_select_db($dbname,$link)or die(mysql_error()); $connect = mysql_select_db($dbname,$link)or die(mysql_error()); if($_GET['w']){ $su=$_GET['w']; $q=mysql_query("SELECT * from url_table WHERE surl='$su'"); $num_rows = mysql_num_rows($q); if($num_rows>0){
$qryres=mysql_fetch_array($q);
$furl=$qryres['furl'];
$id=$qryres['id'];
$clicks=$qryres['click']+1;
$up=mysql_query("UPDATE `url_table` SET `click` = '$clicks' WHERE `id` =$id");
header('Location: $furl');
}else{
$server_add=$_SERVER['SERVER_NAME'];
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
$server_add="https://".$server_add ;
} else {
$server_add="http://".$server_add;
}
header('Location: $server_add');
}
}
else{
$server_add=$_SERVER['SERVER_NAME'];
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
$server_add="https://".$server_add ;
} else {
$server_add="http://".$server_add;
}
header('Location: $server_add');
}

This u.php will redirect to the corresponding full web URL.
I used window.location to redirect. If you want you can use

     header('Location: http://www.example.com/');

instead of window.location
So from this code I think you may got an idea of create url shortener in php

Download Labs