Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Create Short URL Using PHP

Thursday, Sep 28, 2017

Tags:- HTML PHP


Short URL is very good option to use and maintain instead of long URL not only it looks good but it also saves space.Many websites which have low writing space like twitter then for that kind of website short URLs perfectly helps to save writing space. In this tutorial we will show you how to create short URL using PHP.You may also like create short urls using google api..


See Demo
Create Short URL Using PHP


To Create Short URLs It Takes Only Four Steps:-

  1. Make a HTML file to enter long URL
  2. Make a PHP file to convert long URL into short URL
  3. Make a HTML file to enter short URL
  4. Make a PHP file to show original URL


Step 1.Make a HTML file to enter long URL

We make a HTML file and save it with a name long_url.html

<html>
<body>
 <form method="post" action="save_url.php">
  <input type="text" name="url_value" placeholder="Enter URL">
  <input type="submit" name="short_url">
 </form>
</body>
</html>

In this step we create a form to enter URL and submit the data to save_url.php file.You may also like convert text into url using JavaScript.



Step 2.Make a PHP file to convert long URL into short URL

We make a PHP file and save it with a name save_url.php

// Database Structure 
CREATE TABLE `short_urls` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `long_url` text NOT NULL,
 `short_url` text NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

<?php

$host="localhost";
$username="root";
$password="";
$databasename="sample";

$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);

if(isset($_POST['short_url']))
{
 $url=$_POST["url_value"];
 $short_url=substr(md5($url.mt_rand()),0,8);
 mysql_query("insert into short_urls values('','$url','$short_url')");
 echo "Your New URL Is : http://xyz.com/url.php?u=".$short_url."";
}

?>


In this step we create a table 'short_urls` to store URLs then we simple get the url and create a random string for that url and store it in our table and then display the short url.You may also like detect url on input using jQuery.



Step 3.Make a HTML file to enter short URL

We make a HTML file and save it with a name original_url.html

<html>
<body>
 <form method="post" action="get_url.php">
  <input type="text" name="short_url_value" placeholder="Enter Short URL">
  <input type="submit" name="original_url">
 </form>
</body>
</html>

In this step we create a form to get original url by entering their short url.



Step 4.Make a PHP file to show original URL

We make a PHP file and save it with a name get_url.php

<?php
if(isset($_POST['original_url']))
{
 $url=$_POST["short_url_value"];
 $short_url=substr($url,25);

 $select=mysql_query("select long_url from short_urls where short_url='$short_url'");
  
 while($row=mysql_fetch_array($select))
 {
  echo $row['long_url'];	  
 }
}
?>

In this step we get the short url and then we get url code from short url using substr function then we search the database and get original url using that short url code after that we display the original url.You may also like check url existence using PHP.



Thats all, this is how to create short URL using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me


Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.