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.