问题描述
我刚刚开始学习php,不久前我正在建立一个联系我们表单,它将用户输入发送到我的电子邮件。我多年来一直在想这件事,但我没有得到它。我还希望能够在我的电子邮件中收到用户的输入,并且还能够检测到用户的IP地址。当我提交表单时,我收到了除IP地址以外的其他所有输入,但我使用了localhost。
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".
我试着用< input type =hiddenname =messagevalue =<?php echo $ _SERVER [ REMOTE_ADDR'];?>>
但我在线阅读,最好不要使用< input type =hidden>
并只处理表单处理器脚本中的所有内容。请善意帮助我。
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 = "[email protected]";
$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 = "[email protected]";
$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");
}
这篇关于PHP表单处理器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!