Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,060 members, 7,835,582 topics. Date: Tuesday, 21 May 2024 at 12:03 PM

Create Your Own Url Shortner With PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Create Your Own Url Shortner With PHP (1575 Views)

[Tutorial Post] How To Integrate Paystack Payment System With PHP / A Client Needs Url Shortner Website / 10 Nights Of Cloning Nairaland With PHP (2) (3) (4)

(1) (Reply) (Go Down)

Create Your Own Url Shortner With PHP by Aphatech: 12:30pm On Apr 02, 2016
You may wonder why you always see letters mixed with numbers in shortened URL’s. By having more than ten options (0-9) per digit, we are able to have dramatically more combinations while keeping the code as short as possible.

The characters we’ll be using are the digits 1-9 along with various upper/lowercase letters. I have removed all of the vowels to prevent having links created
which are unintended bad words, and I have removed any characters that could be confused with each other. This gives us a list of about 50 characters available for each digit, which means that with two characters, we have 2,500 possible combinations, 125,000 possibilities with three characters, and a whopping 6.5 million combinations with just four characters!
Planning the Database
Let’s set up the
short_urls
table.

It’s a simple table and the create statement is found below:
Code:
CREATE TABLE IF NOT EXISTS short_urls (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
long_url VARCHAR(255) NOT NULL,
short_code VARBINARY(6) NOT NULL,
date_created INTEGER UNSIGNED NOT NULL,
counter INTEGER UNSIGNED NOT NULL DEFAULT '0',

PRIMARY KEY (id),
KEY short_code (short_code)
)
ENGINE=InnoDB;
We have our standard auto-incrementing primary key and fields for the full URL, the shortened code for the URL (indexed for faster retrieval), a timestamp when the row was created, and the number of times the short URL has been accessed.
Note that the long_url
field has a maximum length of 255 characters, which should be sufficient for most applications. If you need to store longer URLs then you’ll need to change its definition to
TEXT.

Now on to the PHP!
Creating a URL Short Code
The code to create and decode short URL codes will be in a class named ShortUrl. First, let’s look at the code responsible for creating the short codes:
Code:
<?php
class ShortUrl
{
protected static $chars = "123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
protected static $table = "short_urls";
protected static $checkUrlExists = true;

protected $pdo;
protected $timestamp;

public function __construct(PDO $pdo) {
$this->pdo = $pdo;
$this->timestamp = $_SERVER["REQUEST_TIME"];
}

public function urlToShortCode($url) {
if (empty($url)) {
throw new Exception("No URL was supplied."wink;
}

if ($this->validateUrlFormat($url) == false) {
throw new Exception(
"URL does not have a valid format."wink;
}

if (self::$checkUrlExists) {
if (!$this->verifyUrlExists($url)) {
throw new Exception(
"URL does not appear to exist."wink;
}
}

$shortCode = $this->urlExistsInDb($url);
if ($shortCode == false) {
$shortCode = $this->createShortCode($url);
}

return $shortCode;
}

protected function validateUrlFormat($url) {
return filter_var($url, FILTER_VALIDATE_URL,
FILTER_FLAG_HOST_REQUIRED);
}

protected function verifyUrlExists($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

return (!empty($response) && $response != 404);
}

protected function urlExistsInDb($url) {
$query = "SELECT short_code FROM " . self::$table .
" WHERE long_url = :long_url LIMIT 1";
$stmt = $this->pdo->prepare($query);
$params = array(
"long_url" => $url
);
$stmt->execute($params);

$result = $stmt->fetch();
return (empty($result)) ? false : $result["short_code"];
}

protected function createShortCode($url) {
$id = $this->insertUrlInDb($url);
$shortCode = $this->convertIntToShortCode($id);
$this->insertShortCodeInDb($id, $shortCode);
return $shortCod

Check the complete code via http://nct.com.ng/Thread-How-To-Create-Your-Own-URL-Shortener-With-PHP


#TeamNCR carea
Re: Create Your Own Url Shortner With PHP by Nobody: 12:42pm On Apr 02, 2016
plz d source in .zip format be helpful
Re: Create Your Own Url Shortner With PHP by Aphatech: 12:46pm On Apr 02, 2016
crotonite:
plz d source in .zip format be helpful
Request for it via the forum, but once it is available I may also post it here, thanks bro
Re: Create Your Own Url Shortner With PHP by Nobody: 12:52pm On Apr 02, 2016
Aphatech:

Request for it via the forum, but once it is available I may also post it here, thanks bro

Can u help me copy the source code and paste it into a new text file, then zip it and upload it here. please.
i am currently browsing with a java powered techno phone.
Re: Create Your Own Url Shortner With PHP by APHATHEOLOGY(m): 2:00pm On Apr 02, 2016
Am sorry bro, cant upload it directly...
Am really sorry for the incovenience, but u can get it in this post http://nct.com.ng/Thread-URL-Shortner-Script-in-PHP
Re: Create Your Own Url Shortner With PHP by Aphatech: 6:06pm On Apr 24, 2016
Join the Programmers Forum via http://www.nct.com.ng/
#TeamNCT cares

(1) (Reply)

Http Request/response Testing / I Need A Developer Who Can Integrate Paypal & Bitcoin Api On A Donation Website / Java Programer Needed

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.