SynapseIndia WEB Development Assemble a URL Shortener With Jquery, PHP and Mysql
Assemble a URL Shortener with jquery, PHP and Mysql
URL shorteners, for example, Tinyurl and bit.ly are extremely popular nowadays, with their prominence having soar close by Twitter and cell phone use. The thought is a genuinely basic one: acknowledge as enter an unsuitably long URL and give in exchange one that fits into the modest restrictions a cell phone and Twitter's 140-character limit. Subsequently, whenever the abridged URL is clicked, the client is first sent to the shortening administration, at which point the truncated URL is contrasted and a database, the first URL recovered, and the client diverted to the sought area.
Past having the capacity to give the prompt utility of truncating a long URL, URL shortening administrations have the capacity furnish clients with various other helpful gimmicks, for example, clickthrough insights, referrer points of interest, and even data about connection dispersion, for example, who else has specified it on Twitter.
Since such an administration embodies truly a couple of helpful programming lessons, I thought it would be both fun and instructive to make our own particular URL shortening arrangement. We'll utilize a few well known instruments to fabricate the administration, including jquery, PHP and Mysql. You can likewise test out the administration for yourself by exploring to u.wjgilmore.com.
Step 1. Make the Mysql Table
How about we start by making the Mysql table used to store the Urls. At least, this table ought to incorporate segments for putting away the first URL and the contracted moniker. Notwithstanding, it would likewise be helpful to store the creation timestamp and clickthrough measurements, so we'll incorporate those sections too:
Make TABLE urls ( id INTEGER UNSIGNED NOT NULL Auto_increment PRIMARY KEY, url Varchar(500) NOT NULL UNIQUE, shortening Char(5) NOT NULL UNIQUE, created_on TIMESTAMP NOT NULL, clicked INTEGER UNSIGNED NOT NULL DEFAULT 0 );
Since a URL-shortening administration will characteristically invest a considerable measure of time examining the table to figure out if a URL as of now exists, and also endeavoring to find a URL condensing, the related sections ought to be listed to support execution:
Make INDEX urls_url ON urls (url(50)); CREATE INDEX urls_abbreviation ON urls (shortened form);
Step 2. Make the HTML Form
Next we'll make a structure that will acknowledge the URL the client might want to curtail. As structures go, this is a really basic one, comprising of simply a content field and a submit catch. As you'll learn in the following step, jquery's otherworldly capacities permit us to improve the structure with an Ajax-driven processor without really needing to entangle the structure format.
structure id="form_url" name="form_url" action="shorten.php" method="post"> shorten a URL, any Url:
This bit ought to be really clear, aside from maybe the DIV distinguished by reaction. We'll come back to this later in the exercise.
Step 3. Make the PHP Script
With the HTML structure set up, how about we next make the PHP script that will transform the appeal. I want to make and test the PHP script before upgrading the site with Ajax, as this methodology makes it simpler to later confine issues when debugging different parts of the application.
The PHP script (shorten.php) is genuinely clear, taking as enter the gave URL and first counseling the Mysql database to figure out if the URL as of now exists in the urls table. In the event that it does, the related shortened form is returned. Something else, another shortened form is produced and embedded into the database alongside the new URL. Should any mistakes happen along the way, FALSE is returned. Something else, the truncated URL is returned. Here's the script:
<?php define("service", "http://u.wjgilmore.com/"); $db = new mysqli("localhost", "webuser", "test", "shortener"); $url = filter_var($_post['url'], Filter_validate_url);/ If filter_var accepted the URL, proceed if ($url) { $stmt = $db->prepare("select condensing FROM urls WHERE url =?"); $stmt->bind_param("s", $url); $stmt->execute(); $stmt->store_result();/ Does the URL as of now exist? Provided that this is true, give back where its due condensing if ($stmt->num_rows == 1) { $stmt->bind_result($abbreviation); $stmt->fetch(); reverberation Service.$abbreviation;/ URL does not as of now exist in DB, so produce a contraction } else { $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; $abbreviation = substr(str_shuffle($chars),0,5); $stmt = $db->prepare("insert INTO urls VALUES (NULL,?, NULL, 0)"); $stmt->bind_param('ss', $url, $abbreviation); if ($stmt->execute()) { reverberation Service.$abbreviation; } else { reverberation "FALSE"; } else { reverberation "FALSE"; }?>
URL shorteners, for example, Tinyurl and bit.ly are extremely popular nowadays, with their prominence having soar close by Twitter and cell phone use. The thought is a genuinely basic one: acknowledge as enter an unsuitably long URL and give in exchange one that fits into the modest restrictions a cell phone and Twitter's 140-character limit. Subsequently, whenever the abridged URL is clicked, the client is first sent to the shortening administration, at which point the truncated URL is contrasted and a database, the first URL recovered, and the client diverted to the sought area.
Past having the capacity to give the prompt utility of truncating a long URL, URL shortening administrations have the capacity furnish clients with various other helpful gimmicks, for example, clickthrough insights, referrer points of interest, and even data about connection dispersion, for example, who else has specified it on Twitter.
Since such an administration embodies truly a couple of helpful programming lessons, I thought it would be both fun and instructive to make our own particular URL shortening arrangement. We'll utilize a few well known instruments to fabricate the administration, including jquery, PHP and Mysql. You can likewise test out the administration for yourself by exploring to u.wjgilmore.com.
Step 1. Make the Mysql Table
How about we start by making the Mysql table used to store the Urls. At least, this table ought to incorporate segments for putting away the first URL and the contracted moniker. Notwithstanding, it would likewise be helpful to store the creation timestamp and clickthrough measurements, so we'll incorporate those sections too:
Make TABLE urls ( id INTEGER UNSIGNED NOT NULL Auto_increment PRIMARY KEY, url Varchar(500) NOT NULL UNIQUE, shortening Char(5) NOT NULL UNIQUE, created_on TIMESTAMP NOT NULL, clicked INTEGER UNSIGNED NOT NULL DEFAULT 0 );
Since a URL-shortening administration will characteristically invest a considerable measure of time examining the table to figure out if a URL as of now exists, and also endeavoring to find a URL condensing, the related sections ought to be listed to support execution:
Make INDEX urls_url ON urls (url(50)); CREATE INDEX urls_abbreviation ON urls (shortened form);
Step 2. Make the HTML Form
Next we'll make a structure that will acknowledge the URL the client might want to curtail. As structures go, this is a really basic one, comprising of simply a content field and a submit catch. As you'll learn in the following step, jquery's otherworldly capacities permit us to improve the structure with an Ajax-driven processor without really needing to entangle the structure format.
structure id="form_url" name="form_url" action="shorten.php" method="post"> shorten a URL, any Url:
This bit ought to be really clear, aside from maybe the DIV distinguished by reaction. We'll come back to this later in the exercise.
Step 3. Make the PHP Script
With the HTML structure set up, how about we next make the PHP script that will transform the appeal. I want to make and test the PHP script before upgrading the site with Ajax, as this methodology makes it simpler to later confine issues when debugging different parts of the application.
The PHP script (shorten.php) is genuinely clear, taking as enter the gave URL and first counseling the Mysql database to figure out if the URL as of now exists in the urls table. In the event that it does, the related shortened form is returned. Something else, another shortened form is produced and embedded into the database alongside the new URL. Should any mistakes happen along the way, FALSE is returned. Something else, the truncated URL is returned. Here's the script:
<?php define("service", "http://u.wjgilmore.com/"); $db = new mysqli("localhost", "webuser", "test", "shortener"); $url = filter_var($_post['url'], Filter_validate_url);/ If filter_var accepted the URL, proceed if ($url) { $stmt = $db->prepare("select condensing FROM urls WHERE url =?"); $stmt->bind_param("s", $url); $stmt->execute(); $stmt->store_result();/ Does the URL as of now exist? Provided that this is true, give back where its due condensing if ($stmt->num_rows == 1) { $stmt->bind_result($abbreviation); $stmt->fetch(); reverberation Service.$abbreviation;/ URL does not as of now exist in DB, so produce a contraction } else { $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789'; $abbreviation = substr(str_shuffle($chars),0,5); $stmt = $db->prepare("insert INTO urls VALUES (NULL,?, NULL, 0)"); $stmt->bind_param('ss', $url, $abbreviation); if ($stmt->execute()) { reverberation Service.$abbreviation; } else { reverberation "FALSE"; } else { reverberation "FALSE"; }?>