Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php get user county

<?php

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", "
", "	", " ", "-", "_"), 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;
}

?>
Comment

PREVIOUS NEXT
Code Example
Php :: PHP 2-Dimentional array 
Php :: acf looping through post types 
Php :: how to download file from s3 bucket using php 
Php :: validate either one field is required in laravel 
Php :: php object is empty 
Php :: laravel multiple copy record 
Php :: wordpress args 
Php :: wp plugin create 
Php :: php iterate through a loop 
Php :: getimagesize php 
Php :: echo php in html 
Php :: array shift php 
Php :: woocommerce_variation_option_name on frontend 
Php :: laravel restrict route 
Php :: laravel filter get pagiination does not flter Appending To Pagination Links 
Php :: drop foreign key laravel eloquent 
Php :: ternary in php 
Php :: how to go one folder back in __dir__ in php 
Php :: checks if file is empty in php 
Php :: php foreach skip to next 
Php :: multe data on database laravel 
Php :: download npm package 
Php :: how remove column in migration laravel 
Php :: get diff array php 
Php :: sms laravel 
Php :: WordPress Plugin Definition 
Php :: php header content type json 
Php :: IP Authorization php code 
Php :: check multiple roles with Blade directive @can? 
Php :: php after leave page 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =