Home > Article > Daily Programming > How to get the currently visited IP address in PHP
In the previous article "How to obtain browser information in PHP", we showed you how to obtain it through the super global variable $_SERVER in PHP. Then PHP obtains the currently accessed IP address, we can still use $_SERVER to achieve IP acquisition.
IP Address: An Internet Protocol address (IP address) is a numerical label assigned to every device connected to a computer network that communicates using the Internet Protocol. An IP address has two main functions: host or network interface identification and location addressing.
Recommended manual:php complete self-study manual
Below we will combine simple code examples to introduce PHP to obtain the currently accessed IP address. Methods.
The code example is as follows:
<?php //ip是否来自共享互联网 if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip_address = $_SERVER['HTTP_CLIENT_IP']; } //ip是否来自代理 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR']; } //ip是否来自远程地址 else { $ip_address = $_SERVER['REMOTE_ADDR']; } echo $ip_address; ?>
Since we are testing locally, the current IP address obtained:
127.0.0.1
Note: $_SERVER is a An array of information such as headers, paths, and script locations.
Recommended related articles:
1.How to get the user’s IP address in php
2.How does php get the client’s IP address?
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial
This article is about An introduction to the method of obtaining the currently accessed IP address in PHP. It is very simple. I hope it will be helpful to friends in need!
The above is the detailed content of How to get the currently visited IP address in PHP. For more information, please follow other related articles on the PHP Chinese website!