₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,110 members, 8,420,407 topics. Date: Thursday, 04 June 2026 at 06:41 PM

Toggle theme

Aphatech's Posts

Nairaland ForumAphatech's ProfileAphatech's Posts

1 2 3 4 (of 4 pages)

ProgrammingCreate Your Own Url Shortner With PHP by Aphatech(op): 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
WebmastersRe: LIMITED SPACE & HOURS - Put Your Website Ads On Naijaloaded With A 50% Discount by Aphatech: 12:24pm On Apr 02, 2016
Lol grin

1 2 3 4 (of 4 pages)