1

I just started learning php not long ago, and I am currently building a "contact us" form which will send the user input to my email. I have been on this for days thinking I will figure it out, but I am not getting it. I wanna also be able to receive the user's input in my email and also be able to detect the user's IP address. When I submitted the form, I received every other input but the IP address though I used "localhost".

I tried with the <input type="hidden" name="message" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"> but I read online that it's better to do it without the <input type="hidden"> and just process everything in the form processor script. Please kindly help me with this.

<?php
$emailError = "";


$messageError = "";




function getUserIp(){

    $client = $_SERVER['HTTP_CLIENT_IP'];

    $forward = $_SERVER['HTTP_X_FORWARD_FOR'];

    $remote = $_SERVER['REMOTE_ADDR'];


    if(filter_var($client, FILTER_VALIDATE_IP)) {

         $ip = $client;

    }elseif(filter_var($forward, FILTER_VALIDATE_IP)) {

         $ip = $client;

    }else{

        $ip = $remote;

    }

 return $ip;

}





if(isset($_POST['submit'])){

    //declares variable
     $email = $_POST["email"];

    $subject = $_POST["subject"];

    $message = $_POST["message"];

  if(empty($_POST['email'])){

    $emailError = "Please enter your email";

  }

  if(empty($_POST['subject'])){

    $subjectError = "Please enter a subject?";

  }
}






if(!empty($_POST['email']) && !empty($_POST['subject'])){

    // Send the email
    $to = "you@yourdomain.com";

    $email = "From: $email";

    $subject = "Subject: $subject";

    $message = "$message" . "\n\n\n==-   Sent from the website with IP Address: " . $ip . "   -==";;

    $headers = "From: $email,";

    $send_contact=mail($to,$email,$subject,$message,$headers);

    header("Location: domain");
}


?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
mindblee
  • 55
  • 6
  • post the code or the question will be closed –  Jun 22 '15 at 02:58
  • 1
    [Read a tutorial](https://bootstrapbay.com/blog/working-bootstrap-contact-form/) – adeneo Jun 22 '15 at 02:58
  • 2
    missing `$ip =getUserIp();` currently you never call your function, $ip lives inside the function only and cant be used out side.An aside- IP is not as useful information as some people think it is –  Jun 22 '15 at 03:03
  • Please check this question.It might be helpful http://stackoverflow.com/questions/14617156/capturing-users-ip-address-in-php-contact-form – Sandeep Varma Jun 22 '15 at 07:13

1 Answers1

0

change below section --

if(!empty($_POST['email']) && !empty($_POST['subject'])){

        // Send the email
        $to = "you@yourdomain.com";
        $ip =getUserIp();

        $email = "From: $email";

        $subject = "Subject: $subject";

        $message = "$message" . "\n\n\n==-   Sent from the website with IP Address: " . $ip . "   -==";;

        $headers = "From: $email,";

        $send_contact=mail($to,$email,$subject,$message,$headers);

        header("Location: domain");
    }
Abhishek
  • 1,543
  • 3
  • 13
  • 29