Jul 02 2009

Statistics on your URL Shortener.

Hello

A few weeks ago I showed you how to create your own URL-Shortener. I decide to improve it, creating a simple system to control the number of access

First of all you need to add some tables to your data base:

CREATE TABLE access (
  aid INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  urls_uid INTEGER UNSIGNED NOT NULL,
  information_iid INTEGER UNSIGNED NOT NULL,
  date DATE NULL,
  PRIMARY KEY(aid),
  INDEX access_FKIndex1(information_iid),
  INDEX access_FKIndex2(urls_uid)
);

CREATE TABLE information (
  iid INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  ip VARCHAR(255) NULL,
  PRIMARY KEY(iid)
);

After this you should do a modification in your function-little-url.php in the function take_url($url):

function take_lurl($lurl) {
        global $link;
        $q = "SELECT * FROM `urls` WHERE `unique_chars` = ‘".$lurl."’";
        $r = mysql_query($q, $link); // Realiza consulta.
        if(mysql_num_rows($r)>0) {
                 $info = mysql_fetch_array($r);
                 $url = $info["url"]; // Pega endereço real
                 $uid = $info["uid"]; // Pega ID do endereço
                 $ip = $_SERVER[‘REMOTE_ADDR’]; // Pega IP do usuário
                 $qr = "select iid from `information` where ip = ‘".$ip."’" ;
                 $rr = mysql_query($qr, $link);  // Procura se este IP já está no banco de dados
                 if(mysql_num_rows($rr)>0) { // Caso esteja insere somente um acesso novo proveniente deste IP
                        $iid = mysql_result($rr,0,"iid");
                        mysql_query("INSERT INTO `access` (urls_uid,information_iid,date) values (".$uid.", ".$iid.", now())", $link);
                 } else { // Caso não esteja, cria um registro com este IP e após cria um acesso.
                        $qr = mysql_query("INSERT INTO `information` (ip) values (‘".$ip."’)", $link);
                        $iid = mysql_insert_id($qr);
                        mysql_query("INSERT INTO `access` (urls_uid,information_iid,date) values (".$uid.", ".$iid.", now())", $link);
                 }
        } else {
                echo "Sorry, link not found!";
        }
        return $url;
}

In this System there is a table with IPs that already accessed some URL, when a URL is accessed it verify the IP, if it is on the table just create an access to this IP in the new address. Otherwise, add the IP and create the access to the address.

How to create a URL-Shortener with Statistics

Matheus

May 09 2009

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.

Read more »

Apr 29 2009

Damn Small Linux, VirtualBox 2.2, Java

Hello,

A week ago I was thinking about banks autentications, here in Brazil, you have to register your computer to get access to the bank site with your computer. Do this every time you format your hard disk is sux, so i thought, why don’t use a virtual machine with a Damn Small linux and use it to access? It’s a good idea.

I created my virtual machine with virtualbox and downloaded the last version of Damn Small Linux. I started the virtualmachine and i saw it was running from cd. Searching around the internet i discovered it is dedicated to live-cd but you can install it if you want to. In portuguese version I translated this article about how to install damn small linux but in english i don’t have to.

So this is just a tip, if you have to register your computer to do some things, why don’t you install a Damn small linux and use it for this? Other advantage is that you can take it with you, and linux don’t have virus so you don’t have to worry about this kind of security

If you want to install Java to firefox follow this article.

If your mouse don’t work use “xsetup.sh” and choose a mouse that isn’t USB. It works for me, now i have my USB mouse working. (I think it’s something about Virtualmachin)

Matheus