Jump to content

Recommended Posts

Php Whizzes!

 

Need to grab geo ip.

Why these 2 show data differently ?

http://www.geoplugin.net/json.gp?ip=19.117.63.253

http://www.geoplugin.net/php.gp?ip=19.117.63.253

Which one you like ? I like 1st one.

 

Anyway, is this code any good ? Will it misfire ?

	<?php
//https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}
	echo ip_info("119.30.32.215", "Country"); ?><br><?php
echo ip_info("119.30.32.215", "Country Code"); ?><br><?php
echo ip_info("119.30.32.215", "State"); ?><br><?php
echo ip_info("119.30.32.215", "City"); ?><br><?php
echo ip_info("119.30.32.215", "Address"); ?><br><?php
	print_r(ip_info("119.30.32.215", "Location")); // Array ( [city] => Menlo Park [state] => California [country] => United States [country_code] => US [continent] => North America [continent_code] => NA )
	?>
	

 

What you say ?

Lemme see your sample code.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/307965-grabbing-users-geo-ip/
Share on other sites

6 hours ago, phpsane said:

Look at the URLs. Make an educated guess. I know that may be hard for you to do but even you should be able to tell that the two are different, and if you can see what the difference is then maybe, just maybe, you might have a clue.

  • Haha 1
On 12/2/2018 at 2:25 AM, requinix said:

Look at the URLs. Make an educated guess. I know that may be hard for you to do but even you should be able to tell that the two are different, and if you can see what the difference is then maybe, just maybe, you might have a clue.

Ha! Ha! I knew both files were using different languages (based on their file extensions) and don't needed to be educated to figure that out.

It's just I don't know this json thing and couldn't figure-out why they didn't just add each entry on a line by themselves on the php one like the json one. Why switch langs just to have each entry on lines by themselves ? What's the real significance ? Unless ofcourse the json one is doing some other tasks in the background which an open human eye fails to catch.

I couldn't help noticing this example of coding efficiency

$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
     $address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
     $address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));

Build the array in the wrong order then reverse it to output in correct order:confused:

  • Like 1
  • 2 weeks later...
On 12/3/2018 at 11:20 PM, Barand said:

I couldn't help noticing this example of coding efficiency


$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
     $address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
     $address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));

Build the array in the wrong order then reverse it to output in correct order:confused:

Ok. I quit.
I'll have it your way then.
Now, how are you gonna make sure that your members are fromn the country they say they are from ?
Say, a guy from USA registers at your membership site (reg.php) and the reg.php prompts the user for his country and he selects Australia. Now, how you gonna force him to select his country truthfully ?
Have you seen when you try visiting paypal.com then if you're from UK then it redirects you to paypal.co.uk ? Here, the user is not given any choice to select his country. Paypal auto detects the user's country. No surprise it would put an entry for that user in their mysql tbl "UK" as the user's country. Let's try doing something like that. Shall we ? And so, how to do it ?
How-about a sample code from your end ?

@requinix,

You're welcome to show a code sample how you'd do things.

3 minutes ago, phpsane said:

Now, how are you gonna make sure that your members are fromn the country they say they are from ?

You can't.

3 minutes ago, phpsane said:

And so, how to do it ?

Don't ask questions you already know the answers to.

3 minutes ago, requinix said:

You can't.

Don't ask questions you already know the answers to.

Hey ?

I don't know the code how to get php to detect the user's country correctly. Someone told me geoip is not the way to do it as it can be fooled.

So, let us see how you folks would do things.

Edited by phpsane
29 minutes ago, requinix said:

GeoIP is the best you can get in general. For mobile users you can ask them to allow your site to read their GPS location. With Javascript.

Ok then. Might aswell sticck to geoip.

However, Barand thinks the code on my original post is clumsy. Any chance you can weed-out the chaff and show me a better way to do it ?

On 12/3/2018 at 11:20 PM, Barand said:

I couldn't help noticing this example of coding efficiency


$address = array($ipdat->geoplugin_countryName);
if (@strlen($ipdat->geoplugin_regionName) >= 1)
     $address[] = $ipdat->geoplugin_regionName;
if (@strlen($ipdat->geoplugin_city) >= 1)
     $address[] = $ipdat->geoplugin_city;
$output = implode(", ", array_reverse($address));

Build the array in the wrong order then reverse it to output in correct order:confused:

Barand & Requinix,

I found it here:

https://stackoverflow.com/questions/12553160/getting-visitors-country-from-their-ip

What you think now ? How-about trying to fix it since you deem it is not worthy at current state ?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.