Obtain visitor's IP

1

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>
    
asked by Pablo Pérez-Aradros 06.11.2016 в 02:11
source

1 answer

4

You have some serious bugs, such as:

isset($_POST('nombre')){
        $nombre = $_POST('nombre');
    }

Missing, the if and the parentheses () have to be square brackets [] :

if ( isset($_POST['nombre'] ) {
     $nombre = $_POST['nombre'];
}

and it is not checked with isset if it has value, if not, with !empty() :

if ( !empty($_POST['nombre'] ) {
     $nombre = $_POST['nombre'];
}

I'll leave you a simple function to get the user's IP (as long as it is available) and how the rest of the simplified code could be left.

sorpresa.php:

<?php
// Función para obtener la IP del usuario
function get_ip_address() {

    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {

        $ip = $_SERVER['HTTP_CLIENT_IP'];

    } else {

        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

        } else {

            $ip = $_SERVER['REMOTE_ADDR'];
        }
    }
    return $ip;
}

// Comprobamos si se ha enviado el formulario
if (isset($_POST['envio_nombre'])) {

    $nombre = 'Anónimo';

    // Comprobamos si el campo nombre no esta vacio
    if (!empty($_POST['nombre'])) {

        $nombre = $_POST['nombre'];
    }

    $para      = '[email protected]';
    $titulo    = 'IP';
    $mensaje   = 'Nombre: '.$nombre.'  IP:'.get_ip_address(); // <= Recibimos la IP del usuario
    $cabeceras = 'From: [email protected]'."\r\n".
        'Reply-To: [email protected]'."\r\n".
        'X-Mailer: PHP/'.phpversion();

    mail($para, $titulo, $mensaje, $cabeceras);
}

HTML:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Prueba enviar email</title>
    </head>
    <body>
        <h4>INTRODUCE TU NOMBRE</h4>
        <form method="post" action="sorpresa.php">
            <input type="text" name="nombre">
            <br>
            <input type="submit" name="envio_nombre">
        </form>
    </body>
</html>
    
answered by 06.11.2016 / 04:09
source