Good morning I'm trying to create a database to save the "likes" that the user gives. For this I need to save the IP so that the same person does not vote repeatedly.
The problem is that I get the IP but it does not save it (or at least it does not show it) I need help please. I pass the code to you:
alta.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<?php
function getRealIP()
{
if (isset($_SERVER["HTTP_CLIENT_IP"]))
{
return $_SERVER["HTTP_CLIENT_IP"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
{
return $_SERVER["HTTP_X_FORWARDED"];
}
elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
{
return $_SERVER["HTTP_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_FORWARDED"]))
{
return $_SERVER["HTTP_FORWARDED"];
}
else
{
return $_SERVER["REMOTE_ADDR"];
}
}
$ip= getRealIP();
//echo $ip."<br/>";
$_POST['ip']=$ip;
echo $_POST['ip']."ESTE";//hasta aqui, bien
?>
<form method="post" action="altab.php">
Correo:<br/>
<input type="text" name="correo" />
<input type="hidden" id="dir" name="ip" >
<input type="hidden" name="fecha" >
<input type="submit" value="listo">
</form>
<script type="text/javascript">//aqui no entra o no hace nada
var ip=<?php echo $ip; ?>;
alert ("hola");
alert(ip);
document.getElementById("dir").value=ip;
</script>
</body>
</html>
altab.php
<?php
header("Content-Type: text/html;charset=utf-8");
include('includes/conexion.php');//Incluimos la conexión
$acentos = $enlace->query("SET NAMES 'utf8'");
$query="INSERT INTO datos(correo,ip) VALUES
('{$_POST['correo']}','{$_POST['ip']}')";
if (mysqli_query($enlace,$query)){
echo $_POST['ip'];
echo 'hecho<br/><a href="index.php">Volver al Indice</a> <br/>';
}
else{
echo "<p>Hubo algún problema. Inténtelo más tarde</p>"; }
?>
listar.php
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<?php
header("Content-Type: text/html;charset=utf-8");
include('includes/conexion.php');//Incluimos la conexión
$acentos = $enlace->query("SET NAMES 'utf8'");
$query = "SELECT * FROM datos";
if ($result = mysqli_query($enlace,$query)){
while($fila = mysqli_fetch_array($result))
{
echo "correo: ".$fila['correo']."<br/>";
echo "IP: ".$fila['ip']."<br/>";
echo "fecha: ".$fila['fecha']."<br/>";
echo "<hr/>";
}
}
?>
<a href="index.php">Volver al Indice</a> <br/>
</body>
</html>
the database is called "clients" and the table is "data"
CREATE TABLE IF NOT EXISTS 'datos' (
'correo' varchar(50) COLLATE utf8_spanish_ci NOT NULL,
'ip' varchar(15) COLLATE utf8_spanish_ci NOT NULL,
'fecha' varchar(8) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY ('correo')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
Dump data for table datos
INSERT INTO 'datos' ('correo', 'ip', 'fecha') VALUES
('[email protected]', '', ''),
('[email protected]', '', ''),
('[email protected]', '', ''),
('[email protected]', '', '');