How to shorten a url in php?

Last Updated : Apr 15, 2022

IN - 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..

Join With 27,000+ Members In Our Google Group & Get Latest Tutorials

Get our latest tutorials, How-To guides on web development every day right into your inbox.

CHECK OUT THIS TUTORIAL LIVE DEMO →

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


 
  
  
 


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

Chủ Đề