Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php get location of user

//Gets the IP Address from the visitor
$PublicIP = $_SERVER['REMOTE_ADDR'];
//Uses ipinfo.io to get the location of the IP Address, you can use another site but it will probably have a different implementation
$json     = file_get_contents("http://ipinfo.io/$PublicIP/geo");
//Breaks down the JSON object into an array
$json     = json_decode($json, true);
//This variable is the visitor's county
$country  = $json['country'];
//This variable is the visitor's region
$region   = $json['region'];
//This variable is the visitor's city
$city     = $json['city'];
Comment

user location using php

<?PHP
    try
    {
        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 = array('ip' => $ip,
                                'continentCode' => $ip_data->geoplugin_continentCode,
                                'countryCode' => $ip_data->geoplugin_countryCode,
                                'countryName' => $ip_data->geoplugin_countryName,
                                );
            }
            return $result;
        }


        $visitor_details = visitor_country(); // Output Country name [Ex: United States]
        $country = $visitor_details['countryName'];
Comment

PREVIOUS NEXT
Code Example
Php :: total no of occurances in string php 
Php :: call metho din config laravel 
Php :: php array_merge 
Php :: php split string by enter 
Php :: htaccess replace url parameter with slash prameter 
Php :: how to add hour minute seconds in php datetime 
Php :: javascript php variable 
Php :: convert date to reverse date in php 
Php :: array con php 
Php :: php ping test 
Php :: laravel migration remove unique constraint 
Php :: php add string inside string at position 
Php :: laravel db transaction 
Php :: regex to check date format php 
Php :: php get remote file last modified 
Php :: laravel session forget 
Php :: wordpress logout redirect to home 
Php :: laravel middleware route group 
Php :: laravel check if form has errors 
Php :: wordpress display all variables 
Php :: laravel get query output 
Php :: how to run laravel project 
Php :: yii2 a href confirm 
Php :: the requested url was not found on this server. laravel 
Php :: remove autoupdate wordpress 
Php :: date format php 
Php :: how to fetch all defined constant in php 
Php :: how to install redis for php7.4 
Php :: php get content phpinfo without show 
Php :: check network connection php 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =