douhuike3199 2015-06-14 16:57
浏览 93

IP地址不会传递给电子邮件正文

My php form is setup to pull the ip address from the sender. The form generates with no errors...test e-mail received, but does not pass the ip address to the body of the e-mail. What did I do wrong? Here is the php form:

<?php
    $owner_email='info@mywebsitedesign.com';
    //SMTP server settings  
    $host = '';
    $port = '465';//"587";
    $username = '';
    $password = '';

    $subject='My Website Design';
    $user_email='';    
    $message_body='';
    $message_type='html';

    $max_file_size=50;//MB 
    $file_types='/(doc|docx|txt|pdf|zip|rar)$/';
    $error_text='something goes wrong';
    $error_text_filesize='File size must be less than';
    $error_text_filetype='Failed to upload file. This file type is not allowed. Accepted files types: doc, docx, txt, pdf, zip, rar.';

    $private_recaptcha_key='6LeZwukSAAAAACmqrbLmdpvdhC68NLB1c9EA5vzU'; //localhost


    $use_recaptcha=isset( $_POST["recaptcha_challenge_field"]) and isset($_POST["recaptcha_response_field"]);
    $use_smtp=($host=='' or $username=='' or $password=='');
    $max_file_size*=1048576;

    if($owner_email==''){
    die('Attention, recipient e-mail is not set! Please define "owner_email" variable in the MailHanlder.php file.');
    }

    if(preg_match('/^(127\.|192\.168\.)/',$_SERVER['REMOTE_ADDR'])){
    die('Attention, contact form will not work locally! Please upload your template to a live hosting server.');
}

    if($use_recaptcha){
        require_once('recaptchalib.php');
        $resp = recaptcha_check_answer ($private_recaptcha_key,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
        if (!$resp->is_valid){
            die ('wrong captcha');
        }
    }

    if(isset($_POST['name']) and $_POST['name'] != ''){$message_body .= '<p>Name: ' . $_POST['name'] . '</p>' . "
" . '<br>' . "
"; $subject.=$_POST['name'];}
    if(isset($_POST['email']) and $_POST['email'] != ''){$message_body .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "
" . '<br>' . "
"; $user_email=$_POST['email'];}
    if(isset($_POST['company']) and $_POST['company'] != ''){$message_body .= '<p>Company: ' . $_POST['company'] . '</p>' . "
" . '<br>' . "
";}  
    if(isset($_POST['topic']) and $_POST['topic'] != ''){$message_body .= '<p>Topic: ' . $_POST['topic'] . '</p>' . "
" . '<br>' . "
";}  
    if(isset($_POST['ipaddress']) and $_POST['ipaddress'] != ''){$message_body .= '<p>IP Address: ' . $_POST['ipaddress'] . '</p>' . "
" . '<br>' . "
";}
    if(isset($_POST['message']) and $_POST['message'] != ''){$message_body .= '<p>Message: ' . $_POST['message'] . '</p>' . "
";}  
    if(isset($_POST['stripHTML']) and $_POST['stripHTML']=='true'){$message_body = strip_tags($message_body);$message_type='text';}

try{
    include "libmail.php";
    $m= new Mail("utf-8");
    $m->From($user_email);
    $m->To($owner_email);
    $m->Subject($subject);
    $m->Body($message_body,$message_type);
    //$m->log_on(true);

// Function to get the client ip address
function get_client_ip_server() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

    if(isset($_FILES['attachment'])){
    if($_FILES['attachment']['size']>$max_file_size){
        $error_text=$error_text_filesize . ' ' . $max_file_size . 'bytes';
        die($error_text);           
        }else{          
        if(preg_match($file_types,$_FILES['attachment']['name'])){
            $m->Attach($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'],'','attachment');
        }else{
            $error_text=$error_text_filetype;
            die($error_text);               
            }
        }       
    }
if(!$use_smtp){
    $m->smtp_on( $host, $username, $password, $port);
    }

    if($m->Send()){
    die('success');
    }   

}catch(Exception $mail){
die($mail);
}   
?>
  • 写回答

3条回答 默认 最新

  • doy2255 2015-06-14 17:00
    关注

    i think no need of if(isset($_POST['ipaddress']) and $_POST['ipaddress'] != '')

    {$message_body .= '<p>IP Address: ' . $_POST['ipaddress'] . '</p>'
    
    to
    
    {$message_body .= '<p>IP Address: ' .get_client_ip_server() . '</p>'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算