user6428790
user6428790

Reputation:

Automatically locate a user's geographical location in PHP

I want to automatically locate the user's geographical location and IP address in PHP upon registration to my website. How do I go about it?

Upvotes: 4

Views: 3540

Answers (5)

AmosJ
AmosJ

Reputation: 11

with codeigniter, you simply do $ip = $this->input->ip_address() to display your IP address

Upvotes: 0

Vinu vasudev
Vinu vasudev

Reputation: 109

Just Try This code,this might help you. This a stand alon code you can create a simple file and copy this. But you have to create an google api key for this.  
 <?php
    if(isset($_POST['long'*emphasized text*]) && isset($_POST['lat'])){
        $key = 'YOUR-KEY_FROM-GOOGLE-API**strong text**';
        extract($_POST);
        $json = file_get_contents('https://maps.googleapis.com/maps/api/**strong text**geocode/json?latlng='.$lat.','.$long.'&key='.$key);
        $data = json_decode($json,true);
        foreach($data['results'][1]['address_components'] as $numbers){
            if( $numbers['types'][0] == 'administrative_area_level_2'){
                echo $numbers['long_name'];
            }
        }
    }else{
    ?>
    Location: <span id="location"></span>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
    <script>
    if (navigator.geolocation) {
      window.onload = function() {
      var startPos;
      var geoOptions = {
        maximumAge: 5 * 60 * 1000,
        timeout: 10 * 1000
      }

      var geoSuccess = function(position) {
        startPos = position;
        var data = {
            long: startPos.coords.longitude,
            lat: startPos.coords.latitude
        };
        var results = jQuery.ajax({
            type: 'POST',
            url: '#',
            data: data,
            async:false
            }).responseText;
        document.getElementById('location').innerHTML = results;
      };
      var geoError = function(position) {
        console.log('Error occurred. Error code: ' + error.code);
        // error.code can be:
        //   0: unknown error
        //   1: permission denied
        //   2: position unavailable (error response from location provider)
        //   3: timed out
      };

      navigator.geolocation.getCurrentPosition(geoSuccess, geoError, geoOptions);
    };

    }
    else {
      console.log('Geolocation is not supported for this Browser/OS version yet.');
    }
    </script>
    <?php
    }
    ?>

Upvotes: 0

Amit Verma
Amit Verma

Reputation: 41219

ip-api.com provides free xml based api.

To get the ip based location using their api, you may use :

$xml=simplexml_load_file("http://ip-api.com/xml/ipaddress");
echo $xml->RegionName;

Upvotes: 5

SESN
SESN

Reputation: 1283

You can use 3rd party solution like http://ipinfodb.com/ip_location_api.php

You can also use the following code:

<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
?>

Upvotes: 0

Peternak Kode Channel
Peternak Kode Channel

Reputation: 586

You can get the IP value by this:

$theClientIp = $_SERVER['REMOTE_HOST'];

then you can make it session like this:

session_start();
$_SESSION['ip']=$theClientIp;

everywhere you can get the ip value by echoing this

session_start(); $_SESSION[ip'];

Upvotes: -1

Related Questions