在php中自带了一个非常的简单的获取IP地址的全局变量,很多初学都获取IP都使用它了,但是对于这些我们一般用法是满足了,但是对于要求高精度这个函数还是不行的。
这个是最简单的方法,对于开了透明代理之类的是没有办法的,如果内网访问也不能读取正确的外网IP,不过很省力就是了:
代码如下 | 复制代码 |
$ip = $_SERVER["REMOTE_ADDR"]; |
搞定~
上面方法用来取得客户端的 IP 地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 IP 地址,而不是真正的客户端 IP 地址
要想透过代理服务器取得客户端的真实 IP 地址,就要使用 getenv("HTTP_X_FORWARDED_FOR") 来读取。
但是如果客户端没有通过代理服务器来访问,那么用getenv("HTTP_X_FORWARDED_FOR") 取到的值将是空的。
代码如下 | 复制代码 |
else if(getenv("HTTP_X_FORWARDED_FOR")) |
表示如果getenv("HTTP_X_FORWARDED_FOR") 取到的值存在不为空(即客户端使用代理服务器的情况下),则变量$ip等于getenv("HTTP_X_FORWARDED_FOR") 取到的真实IP值。
如果上面的else if(getenv("HTTP_X_FORWARDED_FOR"))取得的值为空(即没有使用代理服务器),则不会执行下面的$ip = getenv("HTTP_X_FORWARDED_FOR");这一行语句。
这种情况下已经确认客户端没有使用代理服务器,从而通过
代码如下 | 复制代码 |
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR"); |
这两行语句获得客户端的IP地址也是真实的IP地址,根据经验得出
代码如下 | 复制代码 |
function getIP() |
另外如果我们想获取更精准的使用第三方是一个不错的方法哦
代码如下 | 复制代码 |
function get_onlineip() { |
这样不管理代理还是什么都可以判断IP地址哦

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.