Ir ao conteúdo

Estatísticas no seu TinyURL.

Olá,

Algum tempo atrás eu mostrei como criar um sistema próprio de Tiny-URL, estava curioso para saber como andavam os acessos aos meus links distribuidos por outros endereços, decidi implementar um sistema simples de controle de acessos.

Inicialmente você precisa adicionar estas duas tabelas em seu banco de dados:

[code lang=”sql”]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)
);[/code]

Após isso você deve fazer umas alterações no seu function-little-url.php mais especificamente no take_url($ur) deixe-o da seguinte maneira:

[code lang=”php”]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;
}[/code]

Neste sistema, existe uma tabela de IPs que já acessaram as URLs, quando uma nova URL é consultada, ele verifica se o IP já está cadastrado, caso esteja cria somente um novo acesso para esse IP no endereço X, caso não esteja cadastra este IP e cria o acesso para este IP no endereço X.

Como criar um Tiny URL com suporte para estatisticas

Matheus

PS: Para consultar os acessos você deve acessar seu banco de dados e realizar consultas manualmente.
PS2: Para seu Tiny URL funcionar, você deve adicionar algumas regras de Rewriting. Você pode ver elas em Criar um sistema próprio de Tiny-URL

Edit: Corrigido erro que não adicionava a data e hora quando o IP já estava na tabela.

Publicado emMySQLphp

64 Comentários

  1. Hey there, You’ve performed an incredible job. I’ll certainly digg it and in my opinion suggest to my friends. I am sure they will be benefited from this website.

  2. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say wonderful blog!

  3. Wow that was unusual. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say fantastic blog!

  4. Hwa Hwa

    My partner and I stumbled over here from a different web address and thought I might as well check things out. I like what I see so now i am following you. Look forward to going over your web page yet again.

  5. Hi there, You’ve done an incredible job. I will definitely digg it and in my opinion suggest to my friends. I am confident they will be benefited from this web site.

  6. It’s enormous that you are getting ideas from this piece of writing as well as from our dialogue made at this place.

  7. Right now it appears like Drupal is the best blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  8. Hey! I know this is kinda off topic but I was wondering which blog platform are you using for this website? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

  9. Hi there it’s me, I am also visiting this web site daily, this web site is actually pleasant and the people are actually sharing good thoughts.

  10. I switched over from another site and I am honestly glad I did. The game selection is diverse and the withdrawal process is faster than I expected. It feels like a platform that truly listens to its community and keeps improving. Worth every penny spent. vnkeybet

  11. Checking out thgodplus felt like stumbling into a hidden gem where the slots genuinely respect your wallet. The interface flows smoothly and I love how the promotions stack up without hidden tricks. Every login brings a little smile and that is exactly what a reliable gaming friend should do. thgodplus

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *