0

I want to fetch the city name where a visitor is accessing my website. i want to show some results according to the location(city name). I have tried so many codes but they were not fetching the correct city name. programming Language: javascript or Php

Please help.

2 Answers2

0

There is two way to implement it. Either using javascript or php. The problem with javascript is all browsers does not support this feature and required client permission to trace location. If user does not want to share his location then javascript will prevent it. So best way to use php and you can check out http://www.ipinfodb.com/ this url.

This is free ip db list and you can also find php api to implement it. You have to create free account on this site and get a api key and you can use php code like below

    $location_str = file_get_contents("http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>ip=".$_SERVER['REMOTE_ADDR'] );
    echo $location_Str;

you can replace 'ip-country' with 'ip-city' in url if you want city info.

Vicky
  • 603
  • 5
  • 6
0

You can...

André
  • 477
  • 3
  • 11
  • "Pro: very accurate" — It's only as accurate as the location system the browser uses. If it has a GPS, then its very accurate. If it does GeoIP, then it probably isn't. – Quentin Aug 19 '14 at 10:30
  • Yes, you are right. But compared to a geolocation got by IP it is still a very good result, not least because more and more devices come with GPS. (And also it is worthless if the user does not share his location. ;-)) – André Aug 19 '14 at 10:35