Jump to content
phpsane

Grabbing User's Geo Ip

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!

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

If you think that the only difference between those outputs was whether stuff went on separate lines or not then you need to get your eyes checked.

  • Haha 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.