我的PHP邮件脚本显示“country:unknown”

时间:2013-07-28 16:40:42

标签: php geolocation country ip-geolocation

这是代码,在此代码国家/地区由IP地址执行,当它在我的电子邮件地址上发送邮件时,它会显示“country:unknown”,同时它应该发送访问者/发件人的国家/地区名称。

<?php
$fullName=$_REQUEST['fullName'];
$phnNumber=$_REQUEST['phnNumber'];
$enquery=$_REQUEST['enquery'];
$ipaddress = $_SERVER["REMOTE_ADDR"];
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$pipaddress = getenv('HTTP_X_FORWARDED_FOR');
function visitor_country()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    $result  = "Unknown";
    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

    if($ip_data && $ip_data->geoplugin_countryName != null)
    {
        $result = $ip_data->geoplugin_countryName;
    }

    return $result;
}

 $to="matt@mb-adi-drivingschool.co.uk";
   $subject = "Contact Us form Details";
   $message = "\nFull Name : ".$fullName."\n Phone Number:".$phnNumber."\n General Enquiry : ".$enquery."\n IP Address : ".$ipaddress."\n Host name : ".$hostname."\n Proxy IP Address : ".$pipaddress."\n Country : ".visitor_country()." \n ";
   $from="matt@mb-adi-drivingschool.co.uk";
    @mail($to,$subject,$message,"From:$from");
    @header("location:thankyou.php");
?>

1 个答案:

答案 0 :(得分:1)

function visitor_country()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    $result  = "Unknown";
    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

    if($ip_data && $ip_data->geoplugin_countryName != null)
    {
        $result = $ip_data->geoplugin_countryName;
    }

    return $result;
}

您将“未知”设为$result。这里很明显或ip_data没有值,或者$ip_data->geoplugin_countryNamenull