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.

How to shorten a url in php?

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



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



 

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

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.

CHECK OUT THIS TUTORIAL LIVE DEMO →

Latest Tutorials

  • Create Animated Skill Bar Using jQuery, HTML And CSS
  • Simple And Best Responsive Web Design Using CSS Part 2
  • Show Button On Hover Using HTML And CSS
  • Material Design Login Form Using jQuery And CSS
  • Animated Progress Bar Using jQuery And CSS
  • Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
  • Get Website Statistics Using PHP, jQuery And MySQL
  • HTML5 Login And Signup Form With Animation Using jQuery
  • Facebook Style Homepage Design Using HTML And CSS
  • View Webpage In Fullscreen Using jQuery

Learn how to create a custom URL shortener in PHP. This custom URL shortener will be super simple and can be created in about a hour or two if you are a decent coder. Our custom URL shortener will be coded using PHP, HTML, and jQuery. The PHP code will make the URL shortener work on the back end server. The full source code for our custom URL shortener are available at the link below.

The Setup: Let’s Break it Down

For starters, this custom URL shortener we are about to create will be a fully functional website with a super short domain, lesn.me. So at any point you want to view the final product you can go there to check it out.

Our URL shortener will only need a few different files and only about 400 lines of code, not including the jQuery library. The list of the file breakdown is below:

  • include.php – this will hold some the database connection information and some custom simple functions that we will reuse a few times.
  • index.php – the one and only page the user will ever see of our website. It will have the form the user will submit and where their custom generated shortened url will be displayed for them.
  • process.php – this file will handle the back-end database processing for the form. Having this code in a separate file is crucial for sing jQuery and javascript to submit the form.
  • .htaccess – this file will allow us to have our custom coded urls (e.g. https://lesn.me/iX7yzJc), so we can use random unique codes to redirect to the appropriate URLs.
  • main.css – the primary stylesheet for the site, just enough to make it look at least a little pretty.
  • jquery.js – the jQuery library will allow us to submit our form using javascript. If you want to learn more about jQuery, look at official jQuery documentation site, it is jam packed with useful info.

Create the database table structure

For this url shortener, we will have a pretty simple database structure. Create a new table in phpMyAdmin named “short_links” with 5 columns. In the following order, name the rows: id, code, url, count, and timestamp. Be sure to set the id to a primary index with an auto increment.

How to shorten a url in php?

include.php – defining all the global variables and custom functions

Let’s start by creating our include.php file. The first bit of code here is to just create some global variables that we can use to connect to the database and set some general site info, like the website address. The reason I am using GLOBAL type variables is so that these variables can be used across all the files/scripts that are linked to this include.php script. Making it so we only have to establish them once (aka “define”).

// define the global variables for the site
define("URL_LENGTH", 7);
define("URL_BASE", 'https://lesn.me/');

// set the global variables for the database connection
define("DB_SERVER", 'localhost');
define("DB_USER", 'username');
define("DB_PASS", 'password');
define("DB_NAME", 'lesn_me');

// defene the character set for the url codes to generate from
define("CHARSET", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");

Notice the URL_LENGTH and CHARSET variables:

  • URL_LENGTH – this will determine the overall length of the special code that will be generated when the user created a short url. You don’t want to set it to be too short because you will not be able to support many saved urls. But also not too long, since we want these to be short urls.
  • CHARSET (aka character set) – this string will be used to randomly generate the unique codes. I like using the full character set of upper and lower case letters. This makes it so you can have more options.

Make a custom function to generate our codes

Next, we have our custom function named “generate_code”. As you can imagine, this will generate our random codes used in the url. If you want some clarification on custom functions, you can checkout the official PHP.net documentation.

// function for creating the random string for the unique url
function generate_code(){
	
	// create the charset for the codes and jumble it all up
	$charset = str_shuffle(CHARSET);
	$code = substr($charset, 0, URL_LENGTH);
	
	// verify the code is not taken
	while (count_urls($code) > 0)
		$code = substr($charset, 0, URL_LENGTH);
	
	// return a randomized code of the desired length
	return $code;
}
  • str_shuffle() – we take our master CHARSET and jumble it all around so we can more easily get a random selection of the characters, saving it to the $charset variable.
  • substr() – we then take a sub string that is the exact length we want our codes to be.
  • while loop – then using a while loop, we make sure the generated code is truely unique in our database using a new custom function named “count_urls”. This loop will run for as many times as it takes to generate a unique code, and each time it runs, a new code is automatically generated.

Make a custom function to count custom URLs

Our second custom function named “count_urls” will search in the database to count how many urls have been saved. And because of how the function is setup, we can either get the total number of urls in the database or just the number that have the code that was provided.

// function to count the total number of short urls saved on the site
function count_urls($code = ''){
	// connect to the database
	$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
	
	// build the extra query string to search for a code in the database
	if ($code != '')
		$extra_query = " WHERE code='$code'";
	else
		$extra_query = "";
	
	// count ow many total shortened urls have been made in the database and return it
	$count = (int) mysqli_num_rows(mysqli_query($conn, "SELECT * FROM short_links " . $extra_query));
	return $count;
}
  • first off we connect to the database, in the usual method.
  • $extra_query string – we create this string in the if statement so that we can append the end portion of the SQL query to be run. If the $code parameter was provided to the function when called, the $extra_query string will have the extra SQL query text to specifically search the database for the provided code. If $code was not provided, then the query will just count the total records in the database.
  • $count – simply counting the number of rows that are returned from the SQL query we run. For example, if a $code of “ABCDE” was provided then the SQL query would be “SELECT * FROM short_links WHERE code=’ABCDE’ “

Our last custom function named “validate_urls” is to simply make sure the user has provided a valid url. This function will be used later when we are processing the user’s form submission.

// function to perform all the validation needed for the urls provided
function validate_url($url){
	
	// make sure the user isn't trying to shorten one of our urls
	if (substr($url, 0, strlen(URL_BASE)) != URL_BASE){
		return filter_var($url, FILTER_VALIDATE_URL);
	}
	else
		return false;
}

index.php – the meat and potatoes of the whole show

The index.php file is divided into three portions: the short url redirect section, the javascript, and the form.

index.php – the url redirect section

This portion of the index file is the whole portion of the url redirection. It takes the unique code being supplied in the url and will search the database for the correct record. Then use a header redirect to send the user to the full url that is stored in the database.

 0){
		$code = $_GET['code']; 
		
		// validate the code against the database
		$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
		$query = mysqli_query($conn, "SELECT * FROM short_links WHERE code='$code'");
		if (mysqli_num_rows($query) == 1){
			
			// retrieve the data from the database
			$data = mysqli_fetch_assoc($query);
			
			// update the counter in the database
			mysqli_query($conn, "UPDATE short_links SET count='" . ($data['count']) + 1 . "' WHERE id='". ($data['id'])."'");
			
			/* ADD EXTRA STUFF HERE IF DESIRED */
			
			// set some header data and redirect the user to the url
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
			header("Cache-Control: no-cache");
			header("Pragma: no-cache");

			header("Location: " . $data['url']);
			
			die();
		}
		else
			$message = 'Unable to redirect to your url';
	}
?>
  • require_once() – this will load the include.php script into this file, allowing us to call the functions and use the GLOBAL variables inside of it.
  • if (isset… – with this if statement, we are checking to see if the GET information is being passed to the index.php script. If it is, then we will process the short url request as such.
  • $conn…. – connect to the database and run the search query to find the database record.
  • $data – stores the complete database record for the code provided
  • mysqli… – updates the database counter for the number of views the short url has gotten
  • headers() – sets the web page’s headers and redirects the user to the saved url. It is important that the headers be set before any text is displayed on the page. If any text is displayed, then the headers will be unable to redirect the user

index.php – the form


	

Just enter a url and click "lesn me". That's it!

There are currently lesn.me urls.

index.php – the HTML head and javascript section

The next portion is that javascript magic that will handle the form submission to our back-end php script (aka the process.php). It is important to note that all of the code needs to go in the portion of the page. You will also see that jquery and main stylehseet files are linked in the same area.




  • $(document) … – this is the start of the javascript that. all of the javascript is wrapped inside of this function. This will allow the jquery to function properly.
  • $(“#lesnForm”).submit…. – here is where we use the javascript to process the form submission. the “#” symbol notes that the form id is named “lesnForm”.
  • var url …. – get the url the user entered from the text box
  • if ($.trim … – make sure the a url was entered in the form
  • $.post() … – using the jQuery post function lets us submit the form without having to reload the entire page, which means it will look seamless to the user. The first parameter the function takes is the name of the php script to process the data on the back-end. The second parameter is the form data to be submitted to the back-end script, for us this is just the “url” variable.
  • if (data… – here we process the results returned from the back-end script. With this if statement, we do a little bit of string validation. we are making sure that the url the user provided starts with “http://” or “https://”, because if the back-end script returns a url then it was successful. If the data variable contains anything that is not a url, then it is some sort of error message that will be displayed to the user.

If you put it all together and take a look at our custom URL shortener site, you will see our snazzy new site! Now just to make it all work on the back end!

How to shorten a url in php?

process.php – back-end script to handle the form submission

require_once('./include.php');

// verify the url data has been posted to this script
if (isset($_POST['url']) && $_POST['url'] != '')
	$url = $_POST['url']; 
else
	$url = '';
  • require_once() – this will load the include.php script into this file, allowing us to call the functions and use the GLOBAL variables inside of it.
  • if statement – using the isset() function will return a true if the parameter has been created and a false if it has not. How it works for us is when the form has been submitted using the jQuery, the POST data will be set. If the form was not POSTed to this script, then the isset() function will return a false, which means the form was not properly submitted and therefore no “url” string will be provided. When the form is submitted, then we also make sure the “url” field from the form was filled out.

Validate the URL and add to the database

// verify that a url was provided and that it is a valid url
if ($url != '' && strlen($url) > 0){
	if ( validate_url($url) == true ){
		
		// create a connection to the database
		$conn = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
		
		// create all the variables to save in the database
		$id = '';
		$code = generate_code();
		$timestamp = time();
		$count = 0;
		
		$result = mysqli_query($conn, "INSERT INTO short_links VALUES ('$id', '$code', '$url', '$count', '$timestamp')");
		
		// verify that the new record was created
		$query = mysqli_query($conn, "SELECT * FROM short_links WHERE timestamp='$timestamp' AND code='$code'");
		if ($data = mysqli_fetch_assoc($query)){
			/* SUCCESS POINT */
			
			echo URL_BASE . $code;
		}
		else
			echo 'Unable to shorten your url';
	}
	else
		echo 'Please enter a valid url';
}
else
	echo 'No url was found';
  • if ($url…. – perform some basic string validation of user provided info just to make sure a url was actually provided.
  • if (validate_url…. – call to our validate_url() function we created earlier, this will just make sure a valid url was provided (e.g. has the “http” provided)
  • $conn – connect to the database and store the connection in the $conn variable
  • create the database variables – the next few lines are the miscellaneous variables that are used in our database’s table structure.
  • $result – run the query to INSERT the new record into the database. Any time you are using the SQL command of INSERT, you are saving the provided information into the database. But it is very important that the correct order and structure is given when you do it. If you don’t have the structure exactly right, the data will not be saved and you will get an error on the page, and nobody like to see error!
  • $query – now we run a new query to search the database for the record we just created (aka the one that has the exact code and timestamp). Then using the if statement, we get the database row and store it into the $data variable.
  • last but not least, we echo the entire url back to the user. This will make it so we can also display it back to the user using the javascript we used to submit the form.

.htaccess – enable the short urls

Using the rewrite engine in HTACCESS, we can make it so each of our short urls can be processed as normal links using php GET. For instance, when the user navigates to the short link of lesn.me/CODE. The url will be interpreted as going to lesn.me/index.php?code=CODE.


order allow,deny
deny from all


Options +FollowSymLinks
RewriteEngine on

# allow the url codes to be sent as get data
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?code=$1 [NC]

The Custom URL Shortener: lesn.me!

Now with all the code written, we can take a look our completed custom URL shortener in PHP. And get some custom shortened URLs!

How to shorten a url in php?

How do I shorten an existing URL?

You can shorten a URL by using an URL shortener website, which will shrink your URL for free. Popular link shorteners on the internet include Bitly, TinyURL, and Rebrandly. You'll need a premium account on these sites to unlock the full range of link customization options.

How do I shorten a URL in API?

A URL Shortener API, or Application Programming Interface, will allow a developer to integrate with URL shortening services and create applications, or add those functions to existing applications. The best place to find these APIs is in the URL Shortener category in the ProgrammableWeb directory.