How to shorten a url in php?
Last Updated : Apr 15, 2022 Show
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:-
Step 1. Make a HTML file to enter long URLWe 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 URLWe 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 URLWe 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 URLWe 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
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 DownFor 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:
Create the database table structureFor 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. include.php – defining all the global variables and custom functionsLet’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”).
Notice the URL_LENGTH and CHARSET variables:
Make a custom function to generate our codesNext, 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.
Make a custom function to count custom URLsOur 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.
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.
index.php – the meat and potatoes of the whole showThe index.php file is divided into three portions: the short url redirect section, the javascript, and the form. index.php – the url redirect sectionThis 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.
index.php – the form
|