-2

This is the code and in this code country is executed by IP address, and when it sends mail on my email address then it shows up "country: unknown" while it should send visitor's/sender's country name.

<?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");
?>
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

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;
}

You're putting "Unknown" as $result. Is pretty clear here that or ip_data hasn't a value, or that $ip_data->geoplugin_countryName is null

DonCallisto
  • 29,419
  • 9
  • 72
  • 100
  • actually i am new to php, can you please tell me in a little detail about it and also what should i use instead of this, or correction in this script – user2625373 Jul 28 '13 at 16:46
  • @user2625373: let's `var_dump($ip_data)` for more info; than paste us the result – DonCallisto Jul 28 '13 at 17:02
  • @user2625373: copy `echo var_dump($ip_data);` after `@json_decode` and tell me what's the output – DonCallisto Jul 28 '13 at 17:10
  • @user2625373 You're *"new to php"* and you're working with an advanced script as such? Even I, wouldn't even work with a script like yours, unless I understood every part of it, or most of it for that matter. – Funk Forty Niner Jul 28 '13 at 18:17
  • @DonCallisto I agree with you Don about the OP declaring the result variable as unknown (`$result = "Unknown";`). Whether it's the OP's script or not, **"it"** like the "Song", "Remains the Same". ;-) – Funk Forty Niner Jul 28 '13 at 18:22
  • @user2625373 Clue: **"concatenate"**. – Funk Forty Niner Jul 28 '13 at 18:25