Geo-location Identifier

Now it easy to identify where user accessing your website. You can store the location, state and country. Here i’m giving the code to just show all there parameters. How you need customize.

Step1: Create the geonames.php


if ($_GET['type'] == 'JSON') {

if ($_GET['type'] == 'XML') {

function CURL_file_get_contents( $url ){
          $open_file = curl_init();
            // Tell CURL the url to open.
          curl_setopt($open_file, CURLOPT_URL, $url);
            // Tell CURL to return the file in a string.
          curl_setopt($open_file, CURLOPT_RETURNTRANSFER, 1);
            // Execute the CURL command and retrun the page in a string.
          $return_var = curl_exec($open_file);
  		 $abc = json_decode( $return_var);	  
		 $_SESSION['cityname'] 		= $abc->geonames[0]->name;
	     $_SESSION['state'] 		= $abc->geonames[0]->adminName1;
		 $_SESSION['countryName'] 	= $abc->geonames[0]->countryName;
		 echo $return_var;	 


Step2: Create index.php (or) where you need to show the user location details place the code there.

Place this code before </head>

 <script type="text/javascript">

        function main() {

            // Check for geoLocation Support
            if (navigator.geolocation) {
                // YES                                                      
                // snapshot of position: 
                //navigator.geolocation.getCurrentPosition(renderPosition, renderError);
                // updating position:
                navigator.geolocation.watchPosition(renderPosition, renderError);
            } else {
                // NO
                $('#geoResults').html('<p>Your browser does not support geolocation.</p>');

        //jQuery to render the output
        function renderPosition(position) {

            if (!window.count) window.count = 0;
            var urlJSON ='';

            count ++;
            // var urlJSON = '';
            var urlJSON = ''+position.coords.latitude+'&lon='+position.coords.longitude;
            var urlXML  = '' +position.coords.latitude+'&lon='+position.coords.longitude;

            // now get the XML reverse geo data
            $.getJSON(urlJSON, function(json) {

                /* Parse JSON objects */
                $.each(json.geonames,function(i,item) {
                    // get the name - which is the suburb - and update the page
                    $('#jsonResults').html('<p>You live in: ' + + ' '+ item.adminName1 +' '+ item.countryName+'</p>');


        function renderError() {
            $('#georesults').html('<p>The page could not get your location.</p>');


Place this div where you want to show the details

 <div id=”jsonResults”>your location</div>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s