I want to retrieve information like the city, state, and country of a user from their IP address, so that I can display posts according to their location. Is there a good and reliable way to do this in PHP codeigniter? I am using PHP codeigniter for server-side scripting, and MySQL for the database. Thanks
Asked
Active
Viewed 1,817 times
1
-
4Welcome to StackOverflow. This type of question is off-topic for StackOverflow (_and will be probably closed_). Before to post a question on SO you should go through [the tour](https://stackoverflow.com/tour) and then go to the [Help Section](http://stackoverflow.com/help) to read [What types of questions should I avoid asking?](http://stackoverflow.com/help/on-topic). Finally, if you are sure your question fits the rules, read [How to Ask a question on StackOverflow](http://stackoverflow.com/help/how-to-ask) to be able to make a useful, well formed and on-topic question. – gp_sflover Jul 14 '17 at 23:42
-
@gp_sflover How is this "off-topic"? I don't see the actual words *"recommend me a library"* here. The question merely seems worded in way that the OP at least understands they want to do "geolocation" but do not understand the process behind it. Perhaps dropping a link to a library or asking *"Have you tried a library/service? And do you know how to use it?"* would be more productive. Because as far as "the rules" go, it seems you and others do not actually understand what those rules are. Possibly even a duplicate question, but none of you bothered to cast a single vote. – Neil Lunn Jul 16 '17 at 07:55
-
If I were you I would simply search for resources: https://www.google.com/search?q=php+geolocation. When you have questions about a specific chosen method, then you should ask questions about implementing that code instead. – Neil Lunn Jul 16 '17 at 07:58
1 Answers
0
Hi you will need to contact an external service to do this.
In codeigniter you can use $this->input->ip_address()
to get the IP address and you can pass it to a free service like IP Find to get the location information.
<?php
$request_uri = 'https://ipfind.co';
$ip_address = $this->input->ip_address();
$auth = 'YOUR_API_KEY_HERE';
$url = $request_uri . "?ip=" . $ip_address;
$document = file_get_contents($url);
$result = json_decode($document);
?>
Result:
{
ip_address: "8.8.8.8",
country: "United States",
country_code: "US",
continent: "North America",
continent_code: "NA",
city: null,
county: null,
region: null,
region_code: null,
timezone: null,
owner: null,
longitude: -97.822,
latitude: 37.751,
currency: "USD",
languages: [
"en-US",
"es-US",
"haw",
"fr"
]
}

JordanC
- 4,339
- 1
- 22
- 16