2013-07-28 24 views
-2

這是代碼,在這個代碼國家是通過IP地址執行的,當它發送郵件到我的電子郵件地址時,它顯示「國家:未知」,而它應該發送訪問者/發件人的國家名稱。我的PHP郵件腳本顯示「國家:未知」

<?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="[email protected]"; 
    $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="[email protected]"; 
    @mail($to,$subject,$message,"From:$from"); 
    @header("location:thankyou.php"); 
?> 

回答

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

+0

實際上我是新來的php,能否請你告訴我關於它的一些細節,以及我應該用什麼來代替這個,或者在這個腳本中更正 – user2625373

+0

@ user2625373:讓我們通過'var_dump($ ip_data)'獲取更多信息;比粘貼我們的結果 – DonCallisto

+0

不能理解 – user2625373