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. 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

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

    CandC++ programming training academy in Kanchipuram

  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

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

    Best Graphics Designing Training Academy in Kanchipuram

  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

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

  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

  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

  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

  20. Jika pihak agen melakukan proses penipuan, maka anda yang akan gagal dan rugi sebagai player lho.
    bandar ceme
    paito warna
    forum prediksi


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.