我刚刚开始学习php,我正在建立一个"联系我们"将用户输入发送到我的电子邮件的表单。我已经好几天了,以为我会弄清楚,但我没有得到它。我还希望能够在我的电子邮件中接收用户的输入,并且还能够检测用户的IP地址。当我提交表单时,虽然我使用了#34; localhost"但我收到了所有其他输入但是IP地址。
我尝试使用<input type="hidden" name="message" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
,但我在网上看到,如果没有<input type="hidden">
这样做会更好,只需处理表单处理器脚本中的所有内容。请帮助我。
<?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");
}
?>
答案 0 :(得分:0)
更改以下部分 -
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");
}