{"id":5360,"date":"2023-06-14T04:01:21","date_gmt":"2023-06-14T04:01:21","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=5360"},"modified":"2025-07-25T16:15:34","modified_gmt":"2025-07-25T16:15:34","slug":"get-geolocation-from-ip-address-using-php","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/","title":{"rendered":"Get Geolocation from IP Address using PHP"},"content":{"rendered":"<p><b>IP Geolocation<\/b> helps to know the location of a visitor by IP address. Generally, the IP address is used to get the location of visitors with geolocation information. To get the geolocation, we need to detect the IP address first. Based on the IP address, the location information (country, state, city, etc.) can be retrieved. There are various Third-Party API services available to collect geolocation data from IP addresses. But you can use PHP to get the IP geolocation of visitors without any 3rd Party web service. In this tutorial, we will show you how to detect the user&#8217;s IP address and <b>get geolocation from IP using PHP<\/b>.<\/p>\n<p>The <b>$_SERVER<\/b> variable is an easy option to get the user&#8217;s IP address in PHP. Once the IP address is collected, we will use the GeoIP2 database to get the location of the IP address using PHP. Furthermore, the GeoIP2 library helps to retrieve the geolocation data from the binary Database with PHP.<\/p>\n<p>In this example script, we will do the following operations without depending on any web service or API using PHP.<\/p>\n<ul>\n<li>Get the IP address of the current user.<\/li>\n<li>Get geolocation information from the IP address using PHP.<\/li>\n<li>Detect the visitor&#8217;s country, state (regions), city, and postal code associated with their IPv4 and IPv6 addresses.<\/li>\n<\/ul>\n<h2>Get the IP Address of the Current User<\/h2>\n<p>Use the <code>REMOTE_ADDR<\/code> index of <code>$_SERVER<\/code> to get the current user&#8217;s IP address in PHP.<\/p>\n<pre><span style=\"color: #0000BB\">$ip_addr&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$_SERVER<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"REMOTE_ADDR\"<\/span><span style=\"color: #007700\">];<\/span><\/pre>\n<p>If you want to get location from a specific IP address, specify it in the <code>$ip_addr<\/code> variable.<\/p>\n<pre><span style=\"color: #0000BB\">$ip_addr&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #DD0000\">'154.47.26.248'<\/span><span style=\"color: #007700\">;<\/span> <\/span><\/pre>\n<h2>Get Geolocation from IP Address using PHP<\/h2>\n<p><b>GeoLite2 Geolocation Database:<\/b><br \/>\nBefore getting started, you need to download the latest GeoLite2-City database from the <a href=\"https:\/\/dev.maxmind.com\/geoip\/geolite2-free-geolocation-data\" target=\"_blank\" rel=\"noopener\">MaxMind account<\/a>.<\/p>\n<ul>\n<li>Place this GeoLite2 database binary file (GeoLite2-City.mmdb) in your project directory.<\/li>\n<\/ul>\n<p><b>Download &#038; Install GeoIp2 Library:<\/b><br \/>\nThe GeoIp2 library helps to retrieve the geolocation data from GeoLite2 binary database based on the IP address using PHP.<\/p>\n<p>Run the following command to download and install the GeoIp2 PHP library via Composer.<\/p>\n<pre>composer require geoip2\/geoip2<\/pre>\n<p><span class=\"note\">Note that:<\/span> You don&#8217;t need to download this library separately, all the required files are included in our source code. You can install and access the GeoIp2 library without using Composer.<\/p>\n<p><b>Define Database Reader Object:<\/b><br \/>\nTo parse the GeoLite2 binary database, we need to create a <code>GeoIp2\\Database\\Reader<\/code> object from GeoIp2 class.<\/p>\n<p>Include the autoloader and use the <code>GeoIp2\\Database\\Reader<\/code> namespace to create a Reader object.<\/p>\n<pre><span style=\"color: #007700\">require_once&nbsp;<\/span><span style=\"color: #DD0000\">'vendor\/autoload.php'<\/span><span style=\"color: #007700\">; <br \/>use&nbsp;<\/span><span style=\"color: #0000BB\">GeoIp2<\/span><span style=\"color: #007700\">\\<\/span><span style=\"color: #0000BB\">Database<\/span><span style=\"color: #007700\">\\<\/span><span style=\"color: #0000BB\">Reader<\/span><span style=\"color: #007700\">;<\/span><\/pre>\n<p><b>Retrieve Geolocation data from GeoLite2 Database with PHP:<\/b><\/p>\n<ul>\n<li>In the Reader object, pass the path of the database file as the first argument.<\/li>\n<li>Specify the IP address in the first argument of the <code>city()<\/code> method.<\/li>\n<li>The following geolocation data will be returned from the database based on the given IP address.\n<ul>\n<li>IP Address (<code>traits->network<\/code>)<\/li>\n<li>Country Code (<code>country->isoCode<\/code>)<\/li>\n<li>Country Name (<code>country->name<\/code>)<\/li>\n<li>State Code (<code>mostSpecificSubdivision->isoCode<\/code>)<\/li>\n<li>State Name (<code>mostSpecificSubdivision->name<\/code>)<\/li>\n<li>City Name (<code>city->name<\/code>)<\/li>\n<li>Zipcode (<code>postal->code<\/code>)<\/li>\n<li>Latitude (<code>location->latitude<\/code>)<\/li>\n<li>Longitude (<code>location->longitude<\/code>)<\/li>\n<li>Time Zone (<code>location->timeZone<\/code>)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><span style=\"color: #007700\">try&nbsp;{&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Create&nbsp;a&nbsp;new&nbsp;Reader&nbsp;object&nbsp;and&nbsp;specify&nbsp;the&nbsp;path&nbsp;to&nbsp;the&nbsp;database&nbsp;file&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$cityDbReader&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;new&nbsp;<\/span><span style=\"color: #0000BB\">Reader<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'path\/to\/GeoLite2-City.mmdb'<\/span><span style=\"color: #007700\">);&nbsp; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Get&nbsp;geolocation&nbsp;data&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$record&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$cityDbReader<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">city<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$ip_addr<\/span><span style=\"color: #007700\">);&nbsp; <br \/>}&nbsp;catch(<\/span><span style=\"color: #0000BB\">Exception&nbsp;$e<\/span><span style=\"color: #007700\">)&nbsp;{&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$api_error&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$e<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">getMessage<\/span><span style=\"color: #007700\">();&nbsp; <br \/>}&nbsp; <br \/>&nbsp; <br \/>if(empty(<\/span><span style=\"color: #0000BB\">$api_error<\/span><span style=\"color: #007700\">)){&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$ip_address&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">traits<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">network<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">traits<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">network<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$country_code&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">country<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isoCode<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">country<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isoCode<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$country_name&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">country<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">country<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$state_code&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">mostSpecificSubdivision<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isoCode<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">mostSpecificSubdivision<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">isoCode<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$state_name&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">mostSpecificSubdivision<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">mostSpecificSubdivision<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$city_name&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">city<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">city<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">name<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$zipcode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">postal<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">code<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">postal<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">code<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$latitude&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">latitude<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">latitude<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$longitude&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">longitude<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">longitude<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$time_zone&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;!empty(<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timeZone<\/span><span style=\"color: #007700\">)?<\/span><span style=\"color: #0000BB\">$record<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">location<\/span><span style=\"color: #007700\">-&gt;<\/span><span style=\"color: #0000BB\">timeZone<\/span><span style=\"color: #007700\">:<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">; <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'IP&nbsp;Address:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$ip_address<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Country&nbsp;Code:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$country_code<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Country&nbsp;Name:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$country_name<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'State&nbsp;Code:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$state_code<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'State&nbsp;Name:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$state_name<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'City&nbsp;Name:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$city_name<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Zipcode:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$zipcode<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Latitude:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$latitude<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br\/&gt;'<\/span><span style=\"color: #007700\">;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Longitude:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$longitude<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;br&gt;'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #DD0000\">'Time&nbsp;Zone:&nbsp;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$time_zone<\/span><span style=\"color: #007700\">; <br \/>}else{&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;<\/span><span style=\"color: #0000BB\">$api_error<\/span><span style=\"color: #007700\">;&nbsp; <br \/>}<\/span><\/pre>\n<p class=\"seeAlso\"><span><\/span><a href=\"https:\/\/www.codexworld.com\/get-geolocation-country-latitude-longitude-from-ip-address-using-php\/\">Get Location from IP Address using Geolocation API<\/a><\/span><\/p>\n<h2>Conclusion<\/h2>\n<p>The main advantage of using the GeoLite2 Database is that you can get the location from an IP address free of cost. You don\u2019t need to use any web service or API to get the geolocation of an IP address. Since the location data is fetched from the local database file, there is no 3rd-Party dependency to get Country-State-City-Zipcode-TimeZone details of the IP address. This IP Geolocation script provides an easy way to <b>get the location of an IP address using PHP<\/b>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>IP Geolocation helps to know the location of a visitor by IP address. Generally, the IP address is used to get the location of visitors with geolocation information. To get the geolocation, we need to <\/p>\n","protected":false},"author":1,"featured_media":5364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[343,269,14],"class_list":["post-5360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-geolocation","tag-ip","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Get Geolocation from IP Address using PHP - CodexWorld<\/title>\n<meta name=\"description\" content=\"Get geolocation from IP - Detect the visitor&#039;s IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get Geolocation from IP Address using PHP - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Get geolocation from IP - Detect the visitor&#039;s IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-14T04:01:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-25T16:15:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Get Geolocation from IP Address using PHP\",\"datePublished\":\"2023-06-14T04:01:21+00:00\",\"dateModified\":\"2025-07-25T16:15:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/\"},\"wordCount\":562,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png\",\"keywords\":[\"Geolocation\",\"IP\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/\",\"name\":\"Get Geolocation from IP Address using PHP - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png\",\"datePublished\":\"2023-06-14T04:01:21+00:00\",\"dateModified\":\"2025-07-25T16:15:34+00:00\",\"description\":\"Get geolocation from IP - Detect the visitor's IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2023\\\/06\\\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png\",\"width\":1920,\"height\":1080,\"caption\":\"get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/get-geolocation-from-ip-address-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Get Geolocation from IP Address using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get Geolocation from IP Address using PHP - CodexWorld","description":"Get geolocation from IP - Detect the visitor's IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Get Geolocation from IP Address using PHP - CodexWorld","og_description":"Get geolocation from IP - Detect the visitor's IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.","og_url":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2023-06-14T04:01:21+00:00","article_modified_time":"2025-07-25T16:15:34+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","type":"image\/png"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Get Geolocation from IP Address using PHP","datePublished":"2023-06-14T04:01:21+00:00","dateModified":"2025-07-25T16:15:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/"},"wordCount":562,"commentCount":3,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","keywords":["Geolocation","IP","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/","url":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/","name":"Get Geolocation from IP Address using PHP - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","datePublished":"2023-06-14T04:01:21+00:00","dateModified":"2025-07-25T16:15:34+00:00","description":"Get geolocation from IP - Detect the visitor's IP address and get location from the IP address using PHP. IP geolocation with GeoIp database using PHP.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","width":1920,"height":1080,"caption":"get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/get-geolocation-from-ip-address-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Get Geolocation from IP Address using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2023\/06\/get-geolocation-country-state-city-zipcode-from-ip-address-using-php-codexworld.png","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-1os","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=5360"}],"version-history":[{"count":5,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5360\/revisions"}],"predecessor-version":[{"id":5853,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5360\/revisions\/5853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/5364"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=5360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=5360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=5360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}