Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/11/2015, 18:52
Mazterrr
 
Fecha de Ingreso: noviembre-2009
Ubicación: Puerto Ordaz
Mensajes: 7
Antigüedad: 14 años, 6 meses
Puntos: 0
Exclamación Contador de visitas en php no funciona como deberia

Saludos amigos tengo un contador de visitas que consegui en internet pero no esta trabajando como deberia pues sigue contando las visitas al refrescar la pagina.
Aqui les dejo tal cual como lo tengo funcionando:


TENGO TRES ARCHIVOS:

pagina1.html
pagina2.php
pagina3.php


pagina1.html

Código:
<html> 
<head></head> 
<body> 
<a href="pagina2.php">1 - Creación de la tabla.</a><br> 
<a href="pagina3.php">2 - Página que cuenta la visita</a> 
</body> 
</html>
pagina 2.php
Código:
<?php 
$conexion=mysql_connect("localhost","MI_USUARIO","MI_CLAVE") or 
  die("Problemas en la conexion"); 

mysql_select_db("MI_BASE_DE_DATOS",$conexion) or 
  die("Problemas en la selección de la base de datos"); 

$registros=mysql_query("drop table if exists visitas",$conexion) or 
  die("Problemas en el select:".mysql_error()); 

$registros=mysql_query("create table visitas ( 
                            ip varchar(12), 
                            fecha date, 
                            hora time 
                         )",$conexion) or 
  die("Problemas en el select:".mysql_error()); 

header("Location: pagina1.html"); 
?>
pagina3.php
Código:
<html> 
<head></head> 
<body> 

<?php 
$conexion=mysql_connect("localhost","MI_USUARIO","MI_CLAVE") or 
  die("Problemas en la conexion"); 

mysql_select_db("MI_BASE_DE_DATOS",$conexion) or 
  die("Problemas en la selección de la base de datos"); 

$ip=$_SERVER['REMOTE_ADDR']; 
$fecha=date("Y-m-d"); 
$hora=date("H:i:s"); 
$registro=mysql_query("select * from visitas where ip='$ip' and fecha='$fecha'",$conexion) or  
  die("Problemas en el select:".mysql_error()); 
if ($reg=mysql_fetch_array($registro)) 
{ 
  echo "Usted ya visitó hoy día el sitio.<br>"; 
} 
else 
{ 
  $registros=mysql_query("insert into visitas(ip,fecha,hora) values    ('$ip','$fecha','$hora')",$conexion) 
     or die("Problemas en el select:".mysql_error()); 
} 
$registro=mysql_query("select count(*) as cantidad from visitas",$conexion) or 
  die("Problemas en el select:".mysql_error()); 
$reg=mysql_fetch_array($registro); 
echo "Cantidad de visitas:".$reg['cantidad']; 
?> 
</body> 
</html>
TENGO UNA TABLA LLAMADA "visitas" que esta almacenando lo siguiente:

IP fecha hora
186.89.185.2 2015-11-03 00:34:40
186.89.185.2 2015-11-03 00:34:43
186.89.185.2 2015-11-03 00:34:45
186.89.185.2 2015-11-03 00:34:49

Como les dije cada entrada a pagina3.php esta sumando una visita sin ninguna restriccion.