Short URL is popular for sharing links over social media because it looks tiny. There are many free URL Shortener services available on internet but it's cool to use your own.
In this tutorial I will show you how to create own URL shortening application just using PHP. Oh hell! you heard right, Just using PHP.
Let's Start,
How to Create own URL Shortener System with PHP
We have to create 3 files
- index.php
- url_list.php
- .htacess
You must upload all 3 files in server. Which is available to download below.
First of all lets start with, I set URL of web and default message which is empty.
$baseurl = 'https://pujann.com.np/';
$msg = '';
After that I created function named short_url
function short_url($url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$rand_str = substr(str_shuffle(md5(rand())),0,6);
$oldfile = file_get_contents('url_list.php')."\n";
$newfile = '$list[\''.$rand_str.'\']=\''.$url.'\';';
file_put_contents('url_list.php', $oldfile.$newfile);
return $rand_str;
} else {
return false;
}
}
It will check url is valid or not using filter_var function and later it Generates Random String of 6 digits. I am lazy one so I use file_get_contents and file_put_contents instead of fopen() fwrite(). I stored all file content in oldfile variable and stored new URL and new random string in newfile variable. And finally I put new file after the old file with file_put_content function.
Now Let's check user visited previously created short URL or not if visited redirect them to their respective URL else back to home.
if (isset($_GET['url'])) {
require_once('url_list.php');
if (isset($list[$_GET['url']])) {
$link = $list[$_GET['url']];
header('location:'.$link);
} else {
header('location:'.$baseurl);
}
}
Else if user tries to create short URL then it will call short_url function which we created before to add url on other file and set message.
elseif(isset($_POST['url'])) {
$check = short_url($_POST['url']);
if ($check) {
$msg = "<p class=\"success\">Url Created
<a href=\"{$baseurl}{$check}\">{$baseurl}{$check}</a></p>";
} else {
$msg = "<p class=\"error\">Invalid Url</p>";
}
}
Lets Sum all Codes for index.php file.
<?php
$baseurl = 'http://pujann.com.np/';
$msg = '';
//Lets Short LONG URL
function short_url($url)
{
if (filter_var($url, FILTER_VALIDATE_URL)) {
$rand_str = substr(str_shuffle(md5(rand())),0,6);
$oldfile = file_get_contents('url_list.php')."\n";
$newfile = '$list[\''.$rand_str.'\']=\''.$url.'\';';
file_put_contents('url_list.php', $oldfile.$newfile);
return $rand_str;
} else {
return false;
}
}
//check if user visited
if (isset($_GET['url'])) {
require_once('url_list.php');
if (isset($list[$_GET['url']])) {
$link = $list[$_GET['url']];
header('location:'.$link);
} else {
header('location:'.$baseurl);
}
} elseif (isset($_POST['url'])) {
$check = short_url($_POST['url']);
if ($check) {
$msg = "<p class=\"success\">Url Created
<a href=\"{$baseurl}{$check}\">{$baseurl}{$check}</a></p>";
} else {
$msg = "<p class=\"error\">Invalid Url</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Url Shortener-Pujann</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { padding: 1% 20%; margin: 0 auto; }
.error{color:red; border: 1px solid red; padding: 5px;}
.success{color:green; border: 1px solid green; padding: 5px;}
input{padding: 5px; margin: 0px; background: lightgrey; width: 75%;}
</style>
</head>
<body>
<?php echo $msg;?>
<form action="#" method="post">
<input type="url" name="url" placeholder="Place Long Url eg:https://google.com">
<input type="submit" name="submit" value="Short It">
</form>
<a href="http://pujann.com.np">By Pujann</a>
</body>
</html>
In url_list.php file just put php opening tags, This will keep increasing each time if you used shortener.
<?php
In .htaccess file just put
RewriteEngine On
RewriteRule ^([a-z0-9]{6})$ index.php?url=$1
Done!!!
This much for today... Hope to see you in comment section below.