How to get the client's IP address in php?
1. If no proxy server is used
REMOTE_ADDR = Client IP
HTTP_X_FORWARDED_FOR = No value or not displayed
$ip = $_SERVER['REMOTE_ADDR'];
2. Use transparent proxy
REMOTE_ADDR = Last proxy server IP
HTTP_X_FORWARDED_FOR = client's real IP (when passing through multiple proxy servers, this value is similar: 221.5.252.160, 203.98.182.163, 203.129.72.215)
This type of proxy will also send the client's real IP to the request object , the real IP cannot be hidden.
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
3. Use ordinary anonymous proxy server
REMOTE_ADDR = Last proxy server IP
HTTP_X_FORWARDED_FOR = Proxy server IP (When passing through multiple proxy servers, this value is similar: 203.98.182.163, 203.98.182.163, 203.129.72.215)
This hides the real IP of the client , but the server will know that the client accesses through the proxy server.
4. Use deceptive proxy server
REMOTE_ADDR = Proxy server IP
HTTP_X_FORWARDED_FOR = Random IP (When passing through multiple proxy servers, this value is similar: 220.4.251.159, 203.98.182.163, 203.129.72.215)
The server can identify when passing the proxy server accessed, but the fake IP sent to the target server.
5. Use a high-anonymity proxy
REMOTE_ADDR = Proxy server IP
HTTP_X_FORWARDED_FOR = No The value may not be displayed
When using this kind of proxy, different browsers and different devices will return different IP header information, so PHP uses $_SERVER["REMOTE_ADDR"], $_SERVER[" HTTP_X_FORWARDED_FOR"] The value obtained may be a null value or an "unknown" value.
PHP obtains the ip code as follows:
public function ip() { //strcasecmp 比较两个字符,不区分大小写。返回0,>0,<0。 if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) { $ip = getenv('REMOTE_ADDR'); } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) { $ip = $_SERVER['REMOTE_ADDR']; } $res = preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : ''; echo $res; //dump(phpinfo());//所有PHP配置信息 }
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to get the client's IP address in php?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
