Create URL shortener without database using PHP, demo & download simple PHP URL shortener script and tutorial with PHP example code.
How To Create URL Shortener
We need simple HTML Form, and shortener.php file for processing the request, and we need redirect.php file to redirect the user to a long link, the idea needs to databases, but we will use another way, instead of databases, we will use TXT files. Anyway we need .htaccess file to hide redirect.php file, and we need robots.txt file to hide our TXT database from search engine, our PHP script is safely and simple and works good.
Live Demo
Download & Usage
Download PHP Script, unzip file, open “shortener.php” file, change website link “http://localhost/url-short-txt/” to your website link, upload script files to your hosting, enjoy.
Index
In “index.php” file we have simple HTML Form and PHP Session:
<?php // By Qassim Hassan | wp-time.com session_start(); $_SESSION['prevent_repeat'] = rand(); // Prevent a repeat of the process ?> <form method="post" action="shortener.php"> <p>Enter a long link: <input type="text" name="my-link" value=""></p> <p><input type="submit" value="Shorten!"></p> </form>
Why we use PHP SESSION? To prevent a repeat of the process! after the process is finished, if the user pressed a F5 button (to refresh the page), like this:
Now if the user pressed a “Continue” button, will the process is repeated, will be a lot of files are stored (txt files) and this is a serious problem! with PHP Session we will fix the problem.
Shortener
In “shortener.php” file we have shortener script (read comments):
<?php // By Qassim Hassan | wp-time.com session_start(); if( !empty($_POST['my-link']) and isset($_SESSION['prevent_repeat']) ){ $link = $_POST['my-link']; // link from input "my-link" in index.php form $id = substr( str_shuffle("ASDFGHJKLZXCVBNMQWERTYUIOP0123456789asdfghjklzxcvbnmqwertyuiop"), 0, 6 ); // create random ID (6 lenght), you can change 6 to your custom number $txt_path = "txt-db/"; // txt folder path, you can change it (if you changed it, open robots.txt file and change "txt-db" to your new folder name, robots.txt is file to protection your text database from search engine) if( file_exists($txt_path.$id.".txt") ){ // check if ID is taken while($id) { // start loop to create a new ID $id = substr( str_shuffle("ASDFGHJKLZXCVBNMQWERTYUIOP0123456789asdfghjklzxcvbnmqwertyuiop"), 0, 6 ); // create random ID again (6 lenght) if( file_exists($txt_path.$id.".txt") ){ // if ID is taken continue; // if ID is taken: back again to search for available ID }else{ break; // if found available ID: loop will be finished! } } // end loop } $create_txt_file = file_put_contents($txt_path.$id.".txt", $link); // create a new txt file and add inside it the long link if( $create_txt_file ){ $website = "http://localhost/url-short-txt/"; // enter your website link $short_link = $website.$id; // short link echo 'Your Short Link: <a href="'.$short_link.'" target="_blank">'.$short_link.'</a>'; }else{ echo "We have some errors! Please try later."; } unset($_SESSION['prevent_repeat']); // Prevent a repeat of the process } else{ header("location: index.php"); // if empty link or repeat process, will be redirect the user to index page } ?>
Don’t forget to change “http://localhost/url-short-txt/” to your website link.
Redirect
In “redirect.php” file we will redirect the user to a long link, if short link is invalid or deleted will be redirect the user to index page:
<?php // By Qassim Hassan | wp-time.com if( isset($_GET['id']) ){ $id = $_GET['id']; // short link ID if( file_exists("txt-db/$id.txt") ){ // check if the ID is has txt file in "txt-db" folder $get_long_link = file_get_contents("txt-db/$id.txt"); // get the long link from txt file header("location: $get_long_link"); // redirect the user to the long link! }else{ header("location: index.php"); // if invalid ID (invalid short link or deleted), will be redirect the user to index page } } else{ header("location: index.php"); } ?>
HTACCESS
This “.htaccess” to convert short link from “http://example.com/redirect.php?id=xxxxxx” to “http://example.com/xxxxxx”:
Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteRule ^(\w+)$ ./redirect.php?id=$1
robots.txt
This file “robots.txt” to hide our TXT files from search engine:
User-agent: * Disallow: /txt-db/
Finally, TXT Files will be stored in “txt-db” folder, and in “txt-db” you will find “index.php” file, this file to hide our TXT files from the users.
3 Comments
Tania
Can you add password protection to URL the script creates? If so, how?
Qassim Hassan
Yes! You can add it, create login file.
Robert
Great script!
Could you add the option of having custom URLs as well as generating random ones.
Eg. ‘http://urlshortner.er/customlink’ instead of ‘http://urlshorten.er/a1b2c3’
That would be awesome!