search
HomeBackend DevelopmentPHP ProblemHow to get the client ip address in php

How to get the client ip address in php

How to get the client ip address in php

1. If a proxy server is not used

REMOTE_ADDR = Client IP HTTP_X_FORWARDED_FOR = No value or not displayed (recommended learning: PHP video tutorial)

$ip = $_SERVER['REMOTE_ADDR'];

2. Use transparent proxy

REMOTE_ADDR = Last proxy server IP

HTTP_X_FORWARDED_FOR = Client 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, and 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 client’s real IP, but the server will know that the client goes through the proxy server accessed.

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 the access through the proxy server, but the false IP sent to the target server .

5. Use a high-anonymity proxy

REMOTE_ADDR = Proxy server IP HTTP_X_FORWARDED_FOR = No value or not displayed

Use this When using different proxies, different browsers and different devices will return different IP header information, so the value obtained by PHP using $_SERVER["REMOTE_ADDR"] and $_SERVER["HTTP_X_FORWARDED_FOR"] 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[&#39;REMOTE_ADDR&#39;];
    }
    $res =  preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
    echo $res;
    //dump(phpinfo());//所有PHP配置信息
}

The above is the detailed content of How to get the client 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

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment