Home  >  Article  >  Daily Programming  >  How to get the currently visited IP address in PHP

How to get the currently visited IP address in PHP

藏色散人
藏色散人Original
2019-01-17 13:48:0520141browse

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.

How to get the currently visited IP address in PHP

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[&#39;HTTP_CLIENT_IP&#39;]))
{
    $ip_address = $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
}
//ip是否来自代理
elseif (!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]))
{
    $ip_address = $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;];
}
//ip是否来自远程地址
else
{
    $ip_address = $_SERVER[&#39;REMOTE_ADDR&#39;];
}
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!

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