Jump to content


Photo

Class Contador De Visitas


  • Faça o login para participar
5 replies to this topic

#1 pyro3x

pyro3x

    E

  • Usuários
  • 201 posts
  • Sexo:Masculino

Posted 15/04/2007, 16:57

Descrição:

- Sistema de contador de visitas por ip e data.

Como funciona?
- Verifica se o usuário já visitou.
- Caso tenha visitado não insere nada no banco.
- Caso não tenha visitado, insere nova linha no banco: id,ip,data

<?php
/*
@author : Eduardo Stuart
@version: 1.0
*/

/****************************************
--TABELA:
CREATE TABLE `visitantes` (
`id` INT(30) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
`data` VARCHAR(10) DEFAULT '00/00/0000' NOT NULL, 
`ip` VARCHAR(30) NOT NULL
) TYPE = MYISAM; 
*****************************************/
class visitantes
{
	var $ip;
	var $data;
	var $sql;
	var $hostdb = "localhost";
	var $userdb = "root";
	var $passdb = "";
	var $namedb = "banco";
	var $table  = "visitantes";
	//Pega o ip
	function pegaIP()
	{
		$this->ip = $_SERVER['REMOTE_ADDR'];
		return $this->ip;
	}
	//Pega a data atual
	function pegaData()
	{
		$this->data	= date("d/m/Y");
		return $this->data;
	}
	//Conecta com o banco.
	function conectaDB()
	{
		$conecta = mysql_connect($this->hostdb,$this->userdb,$this->passdb) or die("Erro (1): ".mysql_error());
		mysql_select_db($this->namedb,$conecta) or die(mysql_error());
	}
	//Seleciona o banco
	function selecionaDB()
	{
		$ip	  = $this->pegaIP();
		$data	= $this->pegaData();
		$tabela	= $this->table;
		$this->sql 	= mysql_query("SELECT ip,data FROM $tabela WHERE ip='$ip' AND data='$data'") or die("Erro (2): ".mysql_error());
		return $this->sql;
	}
	//Insere novas visitas
	function insereVisitante()
	{	
		$this->conectaDB();
		$ip	  = $this->pegaIP();
		$tabela	= $this->table;
		$data	= $this->pegaData();

		$insere = mysql_query("INSERT INTO $tabela (id,ip,data) VALUES ('','$ip','$data')") or die("Erro (3): ".mysql_error());
		return $insere;
	}
	//Caso nao tenha visitado = insere novo 
	function jaVisitou()
	{
	
		$this->conectaDB();
		$this->selecionaDB();
		
		if(!mysql_num_rows($this->sql) > 0)
		{
			$this->insereVisitante();
		}		
	}
	//Mostra o total de visitas
	function mostraVisitantes()
	{
		$tabela	= $this->table;
		$this->conectaDB();
		$pegaTodos	= mysql_query("SELECT * FROM $tabela") or die("Erro (4): ".mysql_error());
		$total		= mysql_num_rows($pegaTodos);
		echo $total." visitas";
	}	
	//Chama funcoes
	function chama()
	{
		$this->conectaDB();
		$this->jaVisitou();
		$this->mostraVisitantes();
	}	
}
$visitas = new visitantes();
$visitas->chama();
?>


#2 uiLhian

uiLhian

    Turista

  • Usuários
  • 26 posts
  • Sexo:Não informado

Posted 20/04/2007, 13:08

ta dando erro. :(
Erro (3): Out of range value adjusted for column 'ID' at row 1

#3 pyro3x

pyro3x

    E

  • Usuários
  • 201 posts
  • Sexo:Masculino

Posted 20/04/2007, 15:04

aqui ta funcionando...

http://www.eduardost...om/contador.php

vc criou a tabela? oO

#4 uiLhian

uiLhian

    Turista

  • Usuários
  • 26 posts
  • Sexo:Não informado

Posted 23/04/2007, 09:43

criei...

#5 chipis

chipis

    24 Horas

  • Usuários
  • 439 posts
  • Sexo:Masculino
  • Localidade:Blumenau - SC

Posted 15/06/2007, 09:14

ta dando erro.
Erro (3): Out of range value adjusted for column 'ID' at row 1

mude aqui: VALUES ('',
$insere = mysql_query("INSERT INTO $tabela (id,ip,data) VALUES ('','$ip','$data')") or die("Erro (3): ".mysql_error());
para:VALUES (NULL,
$insere = mysql_query("INSERT INTO $tabela (id,ip,data) VALUES (NULL,'$ip','$data')") or die("Erro (3): ".mysql_error());
aqui tava dando esse erro, alterei e funcionou.
flwss..

Edição feita por: chipis, 15/06/2007, 09:16.


#6 xerxes

xerxes

    Novato no fórum

  • Usuários
  • 2 posts
  • Sexo:Masculino
  • Localidade:Belo Horizonte

Posted 06/08/2007, 11:43

ta dando erro.
Erro (3): Out of range value adjusted for column 'ID' at row 1

mude aqui: VALUES ('',
$insere = mysql_query("INSERT INTO $tabela (id,ip,data) VALUES ('','$ip','$data')") or die("Erro (3): ".mysql_error());
para:VALUES (NULL,
$insere = mysql_query("INSERT INTO $tabela (id,ip,data) VALUES (NULL,'$ip','$data')") or die("Erro (3): ".mysql_error());
aqui tava dando esse erro, alterei e funcionou.
flwss..



interessante

http://www.xermad.com.br/




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq