Jump to content
Sign in to follow this  
anjo2

[PHP] Imagens aleatórias com base de dados para estatisticas

Recommended Posts

anjo2

Bem, a base de dados apenas serve para estatísticas, mas podem criar uma para as imagens ao invés de as terem apenas no php. Eu vou dar o exemplo com 2 imagens.

Criação da tabela:

CREATE TABLE IF NOT EXISTS `nome_da_tabela` (
  `ip` varchar(50) collate latin1_general_ci NOT NULL,
  `views` int(11) NOT NULL,
  `clicks` int(11) NOT NULL,
  `cc` varchar(50) collate latin1_general_ci NOT NULL,
  `country` varchar(50) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`ip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

Ficheiro includes/db.inc.php

<?php
        /*  Database Information - Required!!  */
        /* -- Configure the Variables Below --*/
        $dbhost = 'localhost';
        $dbusername = '';
        $dbpasswd = '';
        $database_name = '';
        /* Database Stuff, do not modify below this line */
        $connection = mysql_connect("$dbhost","$dbusername","$dbpasswd")
                or die ("Couldn't connect to server.");
        $db = mysql_select_db("$database_name", $connection)
                or die(" $database_name - Couldn't select database.");
?>

Ficheiro imagem.php

<?php
        ini_set('display_errors', '0');
        error_reporting (E_ALL); // Report everything
        include("includes/db.inc.php");
        session_start();
        // Add a view
        $ip = $_SERVER['REMOTE_ADDR'];
        $handle = fopen("http://www.ipdomain.info/cc[".$ip."]", "r");
        $cc = stream_get_contents($handle);
        fclose($handle);
        $handle = fopen("http://www.ipdomain.info/country[".$ip."]", "r");
        $country = stream_get_contents($handle);
        fclose($handle);
        if(!mysql_num_rows(mysql_query("SELECT `ip` FROM `nome_da_tabela` WHERE `ip`='$ip'"))) {
                mysql_query("INSERT INTO `nome_da_tabela` values('$ip', 1, 0, '$cc', '$country')");
        }
        else {
                mysql_query("UPDATE `nome_da_tabela` SET `views` = `views` + 1 WHERE `ip`='$ip'");
        }

        header('Content-type: image/gif');
        if(rand(1,2) == 1) {
                $stream = fopen('images/pub1.gif', 'r');
                $_SESSION['image'] = 1;
        }
        else {
                $stream = fopen('images/pub2.gif', 'r');
                $_SESSION['image'] = 2;
        }
        echo stream_get_contents($stream);
        fclose($stream);

?>

Ficheiro visit.php

<?php

        ini_set('display_errors', '0');
        error_reporting (E_ALL); // Report everything
        include("includes/db.inc.php");
        session_start();
        // Add a view
        $ip = $_SERVER['REMOTE_ADDR'];
        $handle = fopen("http://www.ipdomain.info/cc[".$ip."]", "r");
        $cc = stream_get_contents($handle);
        fclose($handle);
        $handle = fopen("http://www.ipdomain.info/country[".$ip."]", "r");
        $country = stream_get_contents($handle);
        fclose($handle);
        if(!mysql_num_rows(mysql_query("SELECT `ip` FROM `nome_da_tabela` WHERE `ip`='$ip'"))) {
                mysql_query("INSERT INTO `nome_da_tabela` values('$ip', 1, 1, '$cc', '$country')");
        }
        else {
                mysql_query("UPDATE `nome_da_tabela` SET `clicks` = `clicks` + 1 WHERE `ip`='$ip'");
        }
        if(!isset($_SESSION['image'])) $link = "http://www.oteusite.com"; // Se não estiver definido
        if($_SESSION['image'] == 1) $link = "http://www.outrosite.com";
        if($_SESSION['image'] == 2) $link = "http://www.outrosite2.com";
        header("Location: $link");

?>

Ficheiro .htaccess

RewriteEngine on
RewriteRule ^pub.gif$ imagem.php [L]
RewriteRule ^page$ visit.php [L]

Agora é só darem o link de www.aminhapagina.com/publicidade/pub.gif e que seja redireccionado para www.aminhapagina.com/page

Share this post


Link to post
Share on other sites
djthyrax

Bom tópico anjo2, bem vindo ao P@P. ;)


Não peças ajuda por PM! A tua dúvida vai ter menos atenção do que se for postada na secção correcta do fórum!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.