Skip to main content

Get IP Geolocation with PHP

There is no build in way or tool in PHP to get geographical location of an IP address. Thats why, we will need to use a third party api to get geographical location of an IP address.

There are many free and paid Geo apis are available today to get geolocation from an IP address. In this post I will use freegeoip.net api. It is very simple and easy. We do not need to register for api key and the most impotent thing is this api is absolutely free!

The ip2location Function

function ip2location($ip)
{
    # api url
    $api = 'http://freegeoip.net/json/' . $ip;

    # connect api with curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    $data = curl_exec($ch);
    curl_close($ch);

    # return reposed data
    return json_decode($data);
}

Usages

$geo = ip2location('8.8.8.8');
$geo->ip;
$geo->country_name;
$geo->region_name;
$geo->city;
$geo->zipcode;
$geo->areacode;

Comments

  1. Thanks for your script! It works great!

    ReplyDelete
  2. Great script, thanks!

    Just a small tip: you should declare your fonction as the following:
    function ip2location($ip = '')
    So that if we don't provide an IP, it will automatically use the client's IP.

    ReplyDelete
  3. You can also use :
    $geo->metro_code;
    $geo->latitude;
    $geo->longitude;

    ReplyDelete
  4. Thanks for your information. I have checked ip address location from this site WhoisXY.com . Here i got ip address location,country,etc..

    ReplyDelete
  5. That was actually really simple i'm used to working with the maxmind.com one this method & api is so much more lightweight.
    thanks for sharing the information

    ReplyDelete
  6. Hi, do you have a real example that could allow to copy past ? I know nothing but I would like to work on it :)
    Many thanks for your help.

    ReplyDelete
  7. Nguyên liệu bằng silicon, mềm mại , nhẹ nhàng và êm tay khi cầm máy,có thể tháo gỡ dễ dàng và nhanh chóng. Đảm bảo hàng mới 100%
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tren-phan-mem-fast-misa.html]học kế toán phần mềm misa fast[/URL]
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tren-phan-mem-excel.html]học kế toán phần mềm excel[/URL]
    [URL=http://lambaocaotaichinh.com/lop-hoc-ke-toan-tong-hop-tai-ninh-binh.html]trung tâm kế toán tại ninh bình[/URL]
    Review: Yeezy Moon Rock & Adidas NMD
    Hai Sản phẩm HOT nhất của Adidas tại thời điểm hiện tại

    ReplyDelete
  8. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.


    SEO Company in Chennai

    ReplyDelete
  9. Shorouk Shorouk for all household services from Cleaning of apartments, cleaning apartments, insect control and sewerage, offering you all the domestic services at the cheapest prices. Contact us to request the service you want Dear customer
    شركة تنظيف بخميس مشيط
    شركة مكافحة حشرات بخميس مشيط
    شركة تنظيف مجالس بخميس مشيط

    شركة تنظيف خزانات بالاحساء
    شركة تنظيف خزانات بابها
    افضل شركة تنظيف خزانات بالاحساء


    ReplyDelete
  10. I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
    vex 3

    ReplyDelete
  11. This is really too useful and have more ideas from yours. keep sharing many things and thanks for sharing the information.

    Best python training institute in Kanchipuram

    ReplyDelete
  12. Nice and informative article. Thanks for sharing such nice article, keep on updating.

    CandC++ programming training academy in Kanchipuram

    ReplyDelete
  13. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...

    Best Java Training Academy in Kanchipuram

    ReplyDelete
  14. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...

    Best Graphics Designing Training Academy in Kanchipuram

    ReplyDelete
  15. Thank you so much for sharing. Keep updating your blog. It will very useful to the many users.
    Best Web Designing Training Academy in Kanchipuram

    ReplyDelete
  16. I have read your blog its very attractive and impressive. I like it your blog.
    Best Software Testing Training Academy in Kanchipuram

    ReplyDelete
  17. Your Blog is nice and informative..Thanks for sharing this valuable information with us..I am visiting this website regularly, this web site sharing nice thoughts

    Best MATLAB Training Academy in Kanchipuram

    ReplyDelete
  18. Thanks for this grateful information. all this information is very important to all the users and can be used good at all this process.


    Best Web Designing Training Academy in Kanchipuram

    ReplyDelete
  19. really you have posted an informative blog. it will be really helpful to many peoples. thank you for sharing this blog. so keep on sharing such kind of useful blogs.

    Best TALLY ERP 9.0 Training Academy In Kanchipuram

    ReplyDelete
  20. Jika pihak agen melakukan proses penipuan, maka anda yang akan gagal dan rugi sebagai player lho.
    asikqq
    pionpoker
    dewaqq
    bandar ceme
    sumoqq
    hobiqq
    paito warna
    interqq
    forum prediksi

    ReplyDelete
  21. Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.
    Best Tally erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  22. Nice..You have clearly explained about it ...Its very useful for me to know about new things..Keep on blogging..
    Best JAVA and J2EE Course Training Institute in kanchipuram|

    ReplyDelete
  23. Hi, am a big follower of your blog. I am really happy to found such a helpful and fascinating post that is written in well manner. Thanks for sharing such an informative post. keep update your blog.
    Best CCNA Course Training Institute in kanchipuram|

    ReplyDelete
  24. Great post.Thanks for one marvelous posting! I enjoyed reading it;The information was very useful.
    Best Best Web Designing and Development Course Training Institute in kanchipuram|

    ReplyDelete
  25. great doing. and i happy to visit your blog to know about this wonderful information. thanks for sharing. keep updating.
    Best Best Web Designing and Development Course Training Institute in kanchipuram|

    ReplyDelete
  26. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    Best Python Course Training Institute in kanchipuram|

    ReplyDelete
  27. Very impressive and informative blog.. thanks for sharing your valuable information.. it is very useful and easy to learn as well... keep rocking and updating... looking further..
    Best Tally Erp9.0 Course Training Institute in kanchipuram|

    ReplyDelete
  28. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    Best Linux Certification Course Training Institute in kanchipuram|

    ReplyDelete
  29. Quite Interesting post!!! Thanks for posting such a useful post. I wish to read your upcoming post to enhance my skill set, keep blogging.
    Best Web Technologies Course Training Institute in kanchipuram|

    ReplyDelete
  30. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated.
    Best Tally ERP 9 Course Training Institute in kanchipuram|

    ReplyDelete
  31. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    Best Graphic Designing Course Training Institute in kanchipuram|

    ReplyDelete
  32. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me
    Best PCB Training Course Training Institute in kanchipuram|

    ReplyDelete
  33. This is excellent information. It is amazing and wonderful to

    visit your site.Thanks for sharing this information, this is useful

    to me…no:1selenium automation training

    institute in kanchipuram.

    ReplyDelete
  34. your blog contain very useful information. Really hereafter I am very big follower of your blog..
    Best MATLAB Course Training Institute in kanchipuram|

    ReplyDelete
  35. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…no:1
    HP QTP / UFT Automation training institute in kanchipuram.

    ReplyDelete
  36. Great post....Thank you for posting the great content……I found it quiet interesting, hopefully you will keep posting such blogs…Keep updating your creative and awesome blog.
    Best Tally Erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  37. The best thing is that your blog really informative thanks for your great information! I have got some important suggestions from it.
    Best Printed Circuit Board (PCB) Course Training Institute in kanchipuram|

    ReplyDelete
  38. The information you have deliver here is really useful to make my knowledge good. Thanks for your heavenly post.
    Best Best Web Designing and Development Course Training Institute in kanchipuram|

    ReplyDelete
  39. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    Best Tally erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  40. Your post explain everything in detail and it was very interesting to read. Thank you sharing.
    Best Tally Erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  41. I am read your post was very interesting good valid information
    keep sharing more information...
    Best Tally Erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  42. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information, this is useful to me…
    Best Tally Erp9 Course Training Institute in kanchipuram|

    ReplyDelete
  43. Really it was an awesome information...very interesting to read..You have provided an nice blog....Thanks for sharing..
    Best JAVA / J2EE / J2ME Course Training Institute in kanchipuram|

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery. Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar.We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script.
In this post I am going to share a simple but useful force file downloader script.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin. It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.