Create URL Shortener PHP Script Without Database

Create URL Shortener PHP Script Without Database
Create URL Shortener PHP Script Without Database

Exthemes Dev Blog – Create URL shortener PHP Without Database, demo & download simple PHP URL shortener script and tutorial with PHP sample code.

How to Create a URL Shortener

We need a simple HTML Form, and a shortener.php file to process the request, and we need a redirect.php file to redirect the user to a long link, the idea needs to be database, but we will use another way, instead of database, we will using .TXT files. After all we need a .htaccess file to hide the redirect.php file, and we need a robots.txt file to hide our TXT database from search engines, our PHP script is safe and simple and works well.

Table Of Contents

How to Usage

Download PHP Script, unzip file, open “shortener.php” file, change website link “https://url.ex-themes.com” to your website link, upload script files to your hosting, enjoy.

Index.php

In “index.php” file we have simple HTML Form and PHP Session:

<?php  
    // By exthemes dev blog | ex-themes.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:

URL shortener PHP,URL Shortener

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

In “shortener.php” file we have shortener script :

<?php
	// By exthemes dev blog | ex-themes.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 = "https://url.ex-themes.com/"; // 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 “https://url.ex-themes.com/” to your website link.

Redirect.php

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 exthemes dev blog | ex-themes.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 “https://url.ex-themes.com/redirect.php?id=xxxxxx” to “https://url.ex-themes.com/xxxxxx”:

#redirect http to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^(\w+)$ ./redirect.php?id=$1


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


Robots txt

This file “robots.txt” to hide our TXT files from search engine:

User-agent: *
Disallow: /txt-db/

Live Demo and Download

url-short-txt.zip3kb

Conclusion

If you want to make Shortener PHP Without Database, you can download my code.

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.

Credits:
wp-time.com

Howdy, I’m exthemes. I’m a web developer living in indonesia. I am a fan of web development, seo, and php. I’m also interested in wordpress and web designer.

You May Like These Posts

No Comments