Skip to content

Creating your own Url shortener.

Good Night,

Here I will show you how to create your own url shortener.

First of all you need to create a table like this:

CREATE TABLE IF NOT EXISTS `urls` (
`uid` int(11) NOT NULL auto_increment,
`url` text default NULL,
`unique_chars` varchar(25) BINARY NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `unique_chars` (`unique_chars`)
);

This code was taken from Abhise in this post "Create your own tinyurl with php and mySQL" that was my bigest reference, from it I took some functions and update other ones to be more efficient. For an example I changed the field to BINARY so it be CASE SENSITIVE (aaaa different from AAAA)

The Abhise says to create many files, I particularly, created one file with all functions where I add all the functions and just called the functions in the files.

We need a fucntion to connect/disconnect to mysql

error_reporting(E_ALL);
$link;
$config;
function connect_db_lurl() {
global $link;
global $config;
$hostname = "localhost";
$username = "USUARIO";
$password = "SENHA";
$dbname = "DATABASE";
$link = mysql_connect($hostname, $username, $password); // Conecta ao mysql.
mysql_select_db($dbname) or die("Unknown database!"); // Seleciona o Banco de dados.
$config["domain"] = "http://seudominio.com"; // Define a configuração da URL inicial
}

function close_db_lurl() {
mysql_close(); // Fecha a conexão com o banco de dados
}

After this I created a function to redirect to the URL.

function redirect($url) {
header("Location:".$url); // Redireciona para a url.
}

Than I used the function from Abhise to generate the char sequence. (I add some chars to elevate the number of combinations

function generate_chars() {
$num_chars = 6; // Tamanho que você deseja as strings
$i = 0;
$my_keys = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Caracteres que valem para formação de endereço
$keys_length = strlen($my_keys);
$url = "";
while($i<$num_chars) { // Gera sequencia aleatoria $rand_num = mt_rand(1, $keys_length-1); $url .= $my_keys[$rand_num]; $i++; } return $url; }

Created a function to verify if the key is unique.

function isUnique($chars) {
global $link;
$q = "SELECT * FROM `urls` WHERE `unique_chars`='".$chars."'";
$r = mysql_query($q, $link); // Verifica se a chave é unica.
if( mysql_num_rows($r)>0 ) {
return false;
} else {
return true;
}
}

And other to verify if the URL is in DB

function isThere($url) {
global $link;
$q = "SELECT * FROM `urls` WHERE `url`='".$url."'";
$r = mysql_query($q); // Verifica se já existe a url
if(mysql_num_rows($r)>0) {
return true;
} else {
return false;
}
}

Function to create

function create() {
global $link;
global $config;
$chars = generate_chars(); // Gera sequencia de caracteres.

while(!isUnique($chars)){ // Verifica se é unico, se não for gera denovo.
$chars = generate_chars();
}

$url = $_GET["u"]; // Pega o endereço que está em ?u=endereço
$url = trim($url); // Retira espaços em branco do inicio e do fim
$url = mysql_real_escape_string($url);

if(!isThere($url)) { // Caso não exista o endereço no banco.
$q = "INSERT INTO `urls` (url, unique_chars) VALUES ('".$url."', '".$chars."')";
$r = mysql_query($q, $link); // Insere o endereço
if(mysql_affected_rows()) {
$q = "SELECT * FROM `urls` WHERE `url`='".$url."'";
$r = mysql_query($q);
$row = mysql_fetch_row($r);
echo $config["domain"]."/".$row[2]; // Imprime endereço para acesso da nova url
} else {
echo "Desculpe, problemas com o banco de dados.";
}
} else { // Caso já exista
$q = "SELECT * FROM `urls` WHERE `url` = '".$url."'";
$r = mysql_query($q); // Seleciona endereço para URL
$row = mysql_fetch_row($r);
echo $config["domain"]."/".$row[2]; // Imprime endereço para acesso da url.
}
}

Looking to the code I thought to create a function to get the url.

function take_lurl($lurl) {
global $link;
$q = "SELECT url FROM `urls` WHERE `unique_chars` = '".$lurl."'";
$r = mysql_query($q, $link); // Pega endereço original para tal string.
if(mysql_num_rows($r)>0) {
$info = mysql_fetch_array($r);
$url = $info["url"];
} else {
echo "Sorry, link not found!";
}
return $url;
}

Created the file "functions-little-url.php" with these functions.

index.php:

create.php:

We need to add some lines do .htaccess and enable mod_rewrite.


RewriteEngine On
RewriteRule ^([1-9a-zA-Z]*)$ index.php\?u=$1 [L]

I did this in my system that uses wordpress (that already uses mod_rewrite) so it work a little bit different. This is my .htaccess


RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f # Verifica se a página acessada não é um arquivo real
RewriteCond %{REQUEST_FILENAME} !-d# Verifica se a página acessada não é um diretório
RewriteRule ^([A-Za-z0-9]{6})$ /lurl/index.php?u=$1 [L] # Caso coincida com a expressão regular redirecione para /lurl/index.php?u=$1 onde /lurl/ é o diretório que está os meus arquivos de tiny-url e [L] indica que é a ultima instrução a ser executada.

# Caso não feche com a parte em cima continua nas regras "padrões" do WordPress
RewriteCond %{REQUEST_FILENAME} !-f # Verifica se a página acessada não é um arquivo real
RewriteCond %{REQUEST_FILENAME} !-d# Verifica se a página acessada não é um diretório
RewriteRule . /index.php [L]

(my URL shortener is inside /lurl/ directory but the redirect was done in matbra.com/XXXXXXX so it redirects to /lurl/

To create URLs acess create.php?u=ADDRESS

URL-Shortener

If you have any problem feel free to contact me.

Best Regards,
Matheus Bratfisch

References:
---- "Tiny Url":
www.php.net
Wynia.org
htmlCenter
---- "Mod Rewrite":
Apache Mod Rewrite

Published inAprendendoMySQLphp

7 Comments

  1. Joao Joao

    Pessoalmente acho melhor você pegar o auto_increment do Mysql e transformar o número inteiro em base alguma coisa. exemplo:

    <?php
    $de = 96408050;
    $str = dec2string($de, 36);
    $dec = string2dec($str, 36);

    echo "
    de = $de
    str = $str
    dec = $dec
    ";

    function dec2string($decimal, $base)
    {
    global $error;

    // $charset = '0123456789abcdefgihjklmnopqrstuvwxyz';
    // $charset = '23456789abcdefghjkmnpqrstuvwxyz'; // 31 = removed 0, o, i, L, 1 (one)
    $charset = '0123456789abcdefgihjklmnopqrstuvwxyz';
    $charset = '0123456789'. // 10
    'abcdefghijklmnopqrstuvwxyz'. // 36
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. // 62
    '_-;.,+~^@$()[]{}'; // 42
    $charset = '0123456789'. 'abcdefghijklmnopqrstuvwxyz';
    $ml = strlen($charset);
    $string = null;
    $base = (int)$base;
    if ($base $ml | $base == 10)
    {
    echo 'BASE must be in the range 2-9 or 11-'. $ml;
    exit;
    }
    $charset = substr($charset, 0, $base);
    if (!ereg('(^[0-9]{1,12}$)', trim($decimal)))
    {
    $error['dec_input'] = 'Value must be a positive integer';
    return false;
    }
    while ($decimal > 0)
    {
    $decimal = (int)$decimal;
    $remainder = ($decimal % $base);
    $char = substr($charset, $remainder, 1);
    $string = "$char$string";
    $decimal = ($decimal - $remainder) / $base;
    }
    return $string;
    }
    function string2dec ($string, $base)
    {
    global $error;

    // $charset = '23456789abcdefghjkmnpqrstuvwxyz';
    // $charset = '0123456789abcdefgihjklmnopqrstuvwxyz';
    $charset = '0123456789'. 'abcdefghijklmnopqrstuvwxyz';
    $ml = strlen($charset);
    $decimal = 0;
    $base = (int)$base;
    if ($base $ml | $base == 10)
    {
    echo 'BASE must be in the range 2-9 or 11-'.$ml;
    exit;
    }

    $charset = substr($charset, 0, $base);
    $string = trim($string);
    if (empty($string))
    {
    $error[] = 'Input string is empty';
    return false;
    }
    while ($string null)
    {
    $char = substr($string, 0, 1);
    $string = substr($string, 1);
    $pos = strpos($charset, $char);
    if ($pos === false)
    {
    $error[] = "Illegal character ($char) in INPUT string";
    return false;
    }
    $decimal = ($decimal * $base) + $pos;
    }
    return $decimal;
    }

    // Call makeCksum once upon landing on the homepage
    function makeCksum()
    {
    $str = "";
    for ($i=0;$i

  2. João,

    Realmente acredito que também seja uma ótima solução, provavelmente melhor que a que eu implementei no dia que fiz esse código já que a mesma não fica tentando criar pois em um banco de dados extenso a criação "aleatória" pode gerar inumeros valores repetidos caindo em algum tipo de loop "semi infinito".

    Bom, para os próximos que forem ler esse tópico fica ai a dica do João, quem sabe assim que eu tiver um tempo pra mecher na implementação desse script (e pretendo fazer algumas modificações) eu adote está solução que também me parece mais viavél.

    Obrigado pela sua sugestão.

  3. [...] Durante a programação também me lembrei que era necessário utilizar algumas regras de Rewritting para fazer o mesmo funcionar e eu estava esquecendo de frisar isso nos outros posts sobre “Tiny-URL”, provavelmente levando muitos usuarios a não conseguirem que o mesmo funcione. Editei os posts antigos colocando um PS, no final. Você pode ver o que deve ser feito no Mod Rewrite no primeiro post sobre este assunto, “Como criar o seu proprio Tiny-URL” [...]

  4. Não consigo usar o sisteminha, pois ponho ele numa pasta e naum funciona.

    Criei a tabela e nada

  5. Voce precisa ter o modrewrite instalado. Precisa configurar ele com o codigo que esta no post no arquivo .htaccess se nao estou enganado.

    Nao eh soh colocar numa pasta e pronto.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.