search
Homephp教程php手册php 比较好的获得用户的ip地址

http://www.nowamagic.net/librarys/veda/detail/144 REMOTE_ADDR只能获取访问者本地连接中设置的IP,如某大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址,如果访问者使用代理服务器,将不获取代理服务器的IP,而是获

http://www.nowamagic.net/librarys/veda/detail/144

REMOTE_ADDR只能获取访问者本地连接中设置的IP,如某大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址,如果访问者使用代理服务器,将不获取代理服务器的IP,而是获取访问者网关的真实IP。如果将这个函数应用到限IP访问的网页中,别人即使通过限IP访问段中的代理服务器,也不能访问该页面。

下面提供一个函数:

<?php // 定义一个函数getIP()
function getIP()
{
	global $ip;
	
	if (getenv("HTTP_CLIENT_IP"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR"))
		$ip = getenv("REMOTE_ADDR");
	else 
		$ip = "Unknow";
		
	return $ip;
}

// 使用方法:
echo getIP();

?>

getenv("REMOTE_ADDR")用来取得客户端的 IP 地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的 IP 地址,而不是真正的客户端 IP 地址。要想透过代理服务器取得客户端的真实 IP 地址,就要使用 getenv("HTTP_X_FORWARDED_FOR") 来读取。

但是如果客户端没有通过代理服务器来访问,那么用getenv("HTTP_X_FORWARDED_FOR") 取到的值将是空的。

else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = 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地址。

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

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.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)