Home Backend Development PHP Tutorial PHP gets user's real IP

PHP gets user's real IP

Aug 08, 2016 am 09:19 AM
http ip server

During the interview a few days ago, I was asked what would be the problem with the IP address obtained through $_SERVER['SERVER_ADDR']? It must be "if the user cannot obtain the real IP address through a proxy". Fortunately, the interviewer did not ask further questions. So how to get the user’s real IP address?

Analysis

Nowadays, many users do not dial-up directly to access the Internet, but access the Internet through routers or other proxies. If we want to obtain the real IP address of the user client, we cannot directly use $_SERVER['SERVER_ADDR'], because the IP address obtained in this way may be a **LAN** IP such as 192.168.1.99, not The user’s public IP address.

Fortunately, when our access request passes through the public network routing or proxy server, the terminal will add the X-Forwarded-For header information to the HTTP request. We can use this request header to obtain the real identity of the client. IP address.

Code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<code><span><span><?php</span><span>/**

 * 获取用户的真实ip地址

 *<span> @return</span> string

 */</span><span><span>function</span><span>get_client_ip</span><span>()</span>{</span><span>$headers</span> = <span>array</span>(<span>'HTTP_X_REAL_FORWARDED_FOR'</span>, <span>'HTTP_X_FORWARDED_FOR'</span>, <span>'HTTP_CLIENT_IP'</span>, <span>'REMOTE_ADDR'</span>);

    <span>foreach</span> (<span>$headers</span><span>as</span><span>$h</span>){

        <span>$ip</span> = <span>$_SERVER</span>[<span>$h</span>];

        <span>// 有些ip可能隐匿,即为unknown</span><span>if</span> ( <span>isset</span>(<span>$ip</span>) && strcasecmp(<span>$ip</span>, <span>'unknown'</span>) ){

            <span>break</span>;

        }

    }

    <span>if</span>( <span>$ip</span> ){

        <span>// 可能通过多个代理,其中第一个为真实ip地址</span><span>list</span>(<span>$ip</span>) = explode(<span>', '</span>, <span>$ip</span>, <span>2</span>);

    }

    <span>/* 如果是服务器自身访问,获取服务器的ip地址(该地址可能是局域网ip)

    if ('127.0.0.1' == $ip){

        $ip = $_SERVER['SERVER_ADDR'];

    }

    */</span><span>return</span><span>$ip</span>;

}

<span>?></span></span></code>

Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces PHP to obtain the user's real IP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Local IP address query Local IP address query Jan 05, 2024 pm 01:42 PM

The methods for querying the IP address of this machine are: 1. Under Windows system, open the "Start Menu" and search for "cmd" to open the command prompt, enter "ipconfig", and then press the Enter key to find a line named "IPv4 Address" , the number next to it is the IP address of the machine; 2. Under macOS system, click the Apple icon in the upper left corner of the screen, select "System Preferences", find the currently connected network in the "Network" option, and click the "Advanced" button , find the IP of the machine in the "TCP/IP" tab, etc.

What does IP mean? What does IP mean? Jan 05, 2024 pm 01:34 PM

The common meanings of IP include "IP address", "intellectual property", "unique selling point" and "independence": 1. IP address is a digital identifier used to identify and locate equipment in computer networks; 2. Intellectual property , refers to the rights and interests enjoyed by people’s intellectual creations, including patents, trademarks, copyrights, trade secrets, etc.; 3. Unique selling points are the unique characteristics of a product or service that distinguish it from competitors and attract customers; 4. Independence refers to the ability of a country or region to make independent decisions and manage itself without external control or interference.

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

See all articles