Home  >  Article  >  Backend Development  >  How to get the client IP address in php? (with code)

How to get the client IP address in php? (with code)

藏色散人
藏色散人Original
2018-11-06 10:18:088648browse

This article mainly introduces how to use PHP to obtain the client's IP address.

First of all, everyone needs to know what an IP address is?

IP Address: An Internet Protocol address (IP address) is a numerical label assigned to every device connected to a computer network that uses the Internet protocol to communicate. An IP address has two main functions: host or network interface identification and location addressing.

Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, due to the growth of the Internet and the exhaustion of available IPv4 addresses, a new version of IP (IPv6) using 128-bit IP addresses was developed in 1995 and standardized in December 1998. In July 2017, the final definition of the agreement was published. IPv6 deployment has been ongoing since the mid-2000s.

IP addresses are usually written and displayed in human-readable notation, such as 172.16.254.2 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing prefix for an address is specified in CIDR notation by appending the number of significant digits following the address, such as 192.168.1.16/24, which is equivalent to the historically used subnet mask of 255.255.255.0.

IP address space is managed globally by the Internet Assigned Numbers Authority (IANA) and five regional Internet registries. They are responsible for distribution to local Internet registries such as end users and local Internet service providers in their designated regions. IANA has allocated IPv4 addresses to RIRs, approximately 16.8 million addresses each. Every ISP or private network administrator assigns an IP address to every device connected to their network.

The following will give you a detailed introduction to how PHP obtains the client IP address.

<?php
if (!empty($_SERVER[&#39;HTTP_CLIENT_IP&#39;]))   
  {
    $ip_address = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
  }
elseif (!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]))  
  {
    $ip_address = $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;];
  }
else
  {
    $ip_address = $_SERVER[&#39;REMOTE_ADDR&#39;];
  }
echo $ip_address;
?>

Then we can use a picture to represent the process of obtaining the IP address:

How to get the client IP address in php? (with code)

This article is about obtaining the client IP address with PHP The method is introduced, I hope it will be helpful to friends in need!

The above is the detailed content of How to get the client IP address in php? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn