简体   繁体   中英

PHP form processor not working

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");
}


?>

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");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM