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 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 = '' . $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);

    # return reposed data
    return json_decode($data);


$geo = ip2location('');


  1. Thanks for your script! It works great!

  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.

  3. You can also use :

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

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

  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.

  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=]học kế toán phần mềm misa fast[/URL]
    [URL=]học kế toán phần mềm excel[/URL]
    [URL=]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

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

    SEO Company in Chennai

  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
    شركة تنظيف بخميس مشيط
    شركة مكافحة حشرات بخميس مشيط
    شركة تنظيف مجالس بخميس مشيط

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

  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

  11. It is the information I am looking for. I'm learning more about this. Hope you bring more things related to it. Thank you very much!

  12. This article is very nice and helpful, I really like this article. We also provide run 3

  13. a pride for me to be able to discuss on a quality website because I just learned to make an article on
    cara menggugurkan kandungan

  14. 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!
    happy wheels

  15. I have read through other blogs, but they are cumbersome and confusing more than your post. I hope you continue to have such quality articles to share with everyone! I believe a lot of people will be surprised to read this article!
    shell shockers

  16. Thank you for this post!! I have just discovered your blog recently and I really like it! I will definitely try some of your insights.
    Best selenium training in chennai
    selenium Classes in chennai

  17. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving. Most of ideas can be nice content.
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

  18. Impressive blog with lovely information. Really very useful article for us thanks for sharing such a wonderful blog...
    Best VMware Training Institute in Chennai | Best VMware Training Institute in Velachery

  19. Great knowledge, Everyone should read this.



Post a Comment

Popular posts from this blog

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code. multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

Live Comment System with jQuery Ajax PHP and MySQL

Recently I have been made a post that focused on how to submit form without page refreshing. That post shows the basic functionality of using jQuery Ajax. On the same follow this post will describe how we can create a comment system to post an instant comment without page refreshing. Let's start...

Resize Image While Uploading with PHP

On my previous post I was shown an example how to upload file with php and html. It was based on php simple image upload functionality. Today I am going to show you little bit advance of image uploading. Suppose if you run a website and want to allow users to upload image files than you need to think little bit about server's space and bandwidth. For this purpose you need to do some optimization with image files. Image resize while uploading is one of them. All major website do it in the same way. If a user uploads a 5mb image file they resize it in different sizes and store on there server. It helps them to seed up there website and reduce there budget. Now take a look, how we can do it ourself.