0

Here is my contact mail PHP code:

<?php
      require "class.phpmailer.php";
      $mail=new PHPMailer();
      $mail->IsSMTP();
      $mail->SMTPDebug = 1;
      $mail->SMTPAuth = true;
      $mail->Host = "domain.mail.com";
      $mail->Port = 000; 
      $mail->Username = 'username@gmail.com';
      $mail->Password = 'pass';
      $mail->SetFrom($mail->Username, $_POST['name']);
      $mail->AddAddress('username@gmail.com', 'username');
      $mail->CharSet = 'UTF-8';
      $mail->Subject = $_POST["topic"];
      $mail->MsgHTML('Name: '.$_POST["name"].'<br/>
                      Subject: '.$_POST["topic"].'<br/>
                      E-Mail: '.$_POST["email"].'<br/>
                      Message: '.$_POST["message"].'<br/>');

      if($mail->Send()) {
          echo "<script>alert('Message successfully sent.');</script>";

          header ("Refresh:0; url=index.html");
      }else { 
          echo  $mail->ErrorInfo;
      }
  }}
?>

My MsgHTML include name, topic, e-mail and message and I want to see IP address of the user. How can I do that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • first of all, **never use raw post data into your code** without proper validation. You **always** have to check if the provided data is valid **before using it**. Then, to answer your question, there are plenty of answers here on SO explaining how to get the user ip with PHP. – ᴄʀᴏᴢᴇᴛ May 28 '18 at 12:23
  • 1
    Possible duplicate of [What is the most accurate way to retrieve a user's correct IP address in PHP?](https://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php) – ᴄʀᴏᴢᴇᴛ May 28 '18 at 12:23

2 Answers2

0

you can get using following function.

 function GetIpAddress()
 {
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check if its shared
    {
       $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
   elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //if ip is from proxxyfrom proxy
     {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
     }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
  }
dipmala
  • 2,003
  • 1
  • 16
  • 17
0

you can use this function to get client IP address

function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
    return $ipaddress;
}
Rajdip Chauhan
  • 345
  • 2
  • 11