I tell you my problem.
I have started to make a program to obtain the IP of a visitor and send an email with this information to a given email.
After searching the internet for several functions and inverting the theme I have not managed to make it work, when I run it gives an error 500 on the server. Here I leave the code to see if any of you can see the fault.
I do not know if the problem is in the code or in the server configuration. Thanks in advance, greetings.
<?php
if( $_SERVER['HTTP_X_FORWARDED_FOR'] != '' )
{
$client_ip =
( !empty($_SERVER['REMOTE_ADDR']) ) ?
$_SERVER['REMOTE_ADDR']
:
( ( !empty($_ENV['REMOTE_ADDR']) ) ?
$_ENV['REMOTE_ADDR']
:
"unknown" );
$entries = preg_split('/[, ]/', $_SERVER['HTTP_X_FORWARDED_FOR']);
reset($entries);
while (list(, $entry) = each($entries))
{
$entry = trim($entry);
if ( preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", $entry, $ip_list) )
{
// http://www.faqs.org/rfcs/rfc1918.html
$private_ip = array(
'/^0\./',
'/^127\.0\.0\.1/',
'/^192\.168\..*/',
'/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/',
'/^10\..*/');
$found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
if ($client_ip != $found_ip)
{
$client_ip = $found_ip;
break;
}
}
}
}
else
{
$client_ip =
( !empty($_SERVER['REMOTE_ADDR']) ) ?
$_SERVER['REMOTE_ADDR']
:
( ( !empty($_ENV['REMOTE_ADDR']) ) ?
$_ENV['REMOTE_ADDR']
:
"unknown" );
}
$nombre = "Anónimo";
isset($_POST('nombre')){
$nombre = $_POST('nombre');
}
var_dump($client_ip);
$para = '[email protected]';
$titulo = 'IP';
$mensaje = 'Nombre:'.$nombre.' IP:'.$ip;
$cabeceras = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($para, $titulo, $mensaje, $cabeceras);
?>
<body>
<h4 style="margin: 0 auto;">INTRODUCE TU NOMBRE </h1>
<form method="post" action="sorpresa.php" style="margin: 0 auto;">
<input type="text" name="nombre" >
</form>
</body>