REMOTE_ADDR for the connection address$_SERVER['REMOTE_ADDR'] contains the network address that connected to your web server. Always provide a fallback because server variables can differ between environments.
<?php
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
if (filter_var($ip, FILTER_VALIDATE_IP)) {
echo 'IP address = ' . htmlspecialchars($ip, ENT_QUOTES, 'UTF-8');
} else {
echo 'IP address is not available.';
}
?>
If your application runs behind a reverse proxy or load balancer, proxy headers must only be trusted when that proxy is under your control.
PHP can read the address associated with the current HTTP connection. IP information is useful for logging, troubleshooting, rate limiting and security auditing, but an IP address should not be treated as a permanent identity for a person.
The basic form is:
<?php
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
echo 'IP address = ' . $ip;
?>
The server currently reports: 216.73.217.99
Headers such as HTTP_X_FORWARDED_FOR can be supplied or altered by clients. Do not automatically treat them as the visitor's real IP address. Only parse a forwarded header when your application is behind a trusted proxy that you configured.
<?php
$connectionIp = $_SERVER['REMOTE_ADDR'] ?? '';
$forwardedFor = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null;
// Trust $forwardedFor only when REMOTE_ADDR is one of your known proxies.
?>
$_SERVER is an associative array containing request and server information. For example, the server name can be read like this:
<?php
$serverName = $_SERVER['SERVER_NAME'] ?? 'unknown';
echo htmlspecialchars($serverName, ENT_QUOTES, 'UTF-8');
?>
To inspect all available entries, use foreach. The old each() function was removed from PHP and should not be used.
<?php
foreach ($_SERVER as $key => $value) {
if (is_scalar($value)) {
echo htmlspecialchars((string) $key, ENT_QUOTES, 'UTF-8')
. ' -> '
. htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8')
. '<br>';
}
}
?>
Many internet connections receive an address dynamically from the ISP, so a visitor's public IP can change. A static address remains assigned to the same connection and is useful for services that require a predictable network endpoint.
inet_pton() converts a valid address to packed binary form, which can be stored in a binary column and later converted back with inet_ntop().
<?php
$packed = inet_pton($ip);
if ($packed !== false) {
// Store $packed in an appropriately sized binary column.
}
?>DNS maps domain names to network addresses. PHP can resolve host names with functions such as gethostbyname(), while checkdnsrr() can test DNS record types.
If an application stores visitor addresses, use a field that supports both IPv4 and IPv6. Store only what your application actually needs.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| John | 15-06-2009 |
| I need a script similar to that of the one at tracemyip.com | |
| anto | 17-06-2009 |
| nice.. can you show how to get the full details for the network addressed including the gateway and sub-net mask? thank you | |
| Spirit | 20-06-2009 |
| You cannot find out somone's subnet mask, or gateway externally. Both are part of their internal network, all of which shares the same external IP. | |
| Myles | 13-07-2009 |
| To John - Thanks for that info, now go and make it. Anto, type ipconfig in msdos or as is probably more apt.....winipcfg in START then RUN. | |
| ajay joshi | 19-09-2009 |
| thanx.. can you show how to get the full details for ip tracing/ip trapping | |
| CanadaWest | 21-09-2009 |
| You say, "recording the geographical location of the visitor can be done this way" How can I get the user's geographical location to redirect the user to the correct info for their region? | |
| smo | 21-09-2009 |
| Once IP address is known then the location can be collected by using available geo location tables. These tables are available at cost and can be updated regularly. | |
| gizmo | 28-11-2009 |
| can you show how to get the full details for the network addressed | |
| santosh | 30-12-2009 |
| how to run the above code?... | |
| Pham Nguyen | 01-01-2010 |
| why do i use the code $ip=@$REMOTE_ADDR; on localhost. $ip=127.0.0.1. Please lest me know how about on the host server? | |
| Krishnat | 31-01-2010 |
| i want to design cyberoam through php..so please tell me some guideline | |
| PHP Developer | 18-03-2010 |
| Looking to find out how to look up who owns the IP and what the IP owner's address is. Thanks! | |
| kiran malvi | 15-04-2010 |
| This ip function are most usefull for anyone. so thanks. | |
| Mahendra Rajgude | 07-07-2010 |
| Really useful info. Thanks. | |
| Mutahir shah | 22-10-2010 |
| Please someone could provide me how to conver IP address to address where the user is loged in. | |
| John | 11-01-2011 |
| Thanks ....it worked. | |
| Michael | 11-02-2011 |
| I have 14 tracts that I want the visitors to my website to be able to send to their friends. Please can you let me have a script attached to be bottom of the page telling visitors to send to their friends. | |
| sparky | 23-08-2012 |
| I am trying to determine where to place this piece of code in my .php file. The page has html and php code inside of it. I would like to track IP addresses and have them stored to my .csv file, which I already have working with fwrite. | |
| Hitender | 05-12-2012 |
| how to fetch the ip address of user filling a form in php sites? I want the exact coding please let me know about this.... | |
| HridoY636 | 03-04-2013 |
| Why those code shows different ips in differnt time.. Even i am visiting this page with same network and same phone and even same browser? Plz answer me. | |
| smo1234 | 06-04-2013 |
| Your ISP assign you different IP each time you start connecting to internet. This is known as dynamic IP. In contrast if you take static IP from your ISP , they will charge more for this but you can have your own services like VOIP, web hosting etc. | |
| akhil | 22-04-2013 |
| i want to know the ip address of the computer, who chat with me from another network ? | |
| Anindya | 18-05-2013 |
| how to fetch permanent IP address of a computer..?? is it possible..?? | |
| George | 19-05-2013 |
| you can also use a ip geo-location pluggin like the one in bricksett cms. it takes users ip address and then gives you the country code. | |
| mohsen | 02-09-2013 |
| thank you... it's short and useful. | |
| FARHAN | 17-03-2015 |
| Can you help me i want full script can you send me my email address please ( thank you for helping me ) | |
| siva | 02-08-2016 |
| Hi i was using above ip program, but my result arrived this format :IP address= ::1 | |
| smo1234 | 10-09-2016 |
| If you can run PHP script , then you can run this code, there is no extra code required. | |