So there are many ways to do that but the easiest way to do that is using API. Now there are tons of API providers which provide API for this and I choose one called IpApi.co.
Why I choose IpApi.co?
I am looking for fast response API and it pretty much fulfill my needs and also it return with many results i.e country, state, city, ip, region, asn, org, latitude, longitude that’s all.
So Lets get started,
So we all know that when we are building a project in localhost and when we fetch ip address by using php function it return correct ip but when we upload our project on the online server the ip returned is not our ip actually its the server ip on which our hosting is based on. so we need to fetch the ip of user’s side. So in this project i have created a separate function which can fetch the user’s ip address instead of our server ip address and this function can easily bypass any kind of proxy’s ip of any vpn or anything else so that we can get user’s original ip address.
So there are two files getip.php in which there is a function for getting the ip and index.php in which we are getting our main stuff i.e user’s details.
So for ipapi.co we need to pass user’s ip and lot of you thinking that it ask for ip and return ip as result? Yes actually it cannot tells ip from users end so we need to pass ip at the API EndPoint .
say your ip is 126.96.36.199.
so the endpoint will be-
and the result we get is –
So this the array form of result of the user’s details
and extra thing that i have included in this project is the google static map according to user latitude and longitude.
How to set it up?
For IpApi, you don’t have to do anything but for google map your have to obtain google static map api key.
- Go to Google Developer Console.
- Go to Library on the left sidebar.
- Now search for static map api
- Click on the google Static Maps API and click enable.
- Go to credentials and copy the API key and paste it on line no. 35 and replace YOUR_GOOGLE_STATIC_MAP_API_KEY with your API key.
- Good to Go.
Note : If you are using localhost to just test it out just remove the ip from the api endpoint url and make it like this –
and you are good to go.
See the full video on how to set it up
That’s it for this tutorial and please subscribe to my blog if you like this post and please share it with your friends and please subscribe my youtube channel.