获取用户真实ip地址的方法
一、没有使用代理服务器的情况:
REMOTE_ADDR = 您的 IP
HTTP_VIA = 没数值或不显示
HTTP_X_FORWARDED_FOR = 没数值或不显示
二、使用透明代理服务器的情况:Transparent Proxies
REMOTE_ADDR = 最后一个代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 您的真实 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。
三、使用普通匿名代理服务器的情况:Anonymous Proxies
REMOTE_ADDR = 最后一个代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 代理服务器 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。
四、使用欺骗性代理服务器的情况:Distorting Proxies
REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 随机的 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
告诉了访问对象您使用了代理服务器,但编造了一个虚假的随机IP代替您的真实IP欺骗它。
五、使用高匿名代理服务器的情况:High Anonymity Proxies (Elite proxies)
REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 没数值或不显示
HTTP_X_FORWARDED_FOR = 没数值或不显示 ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
实例
/** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { // 初始化一个变量$realip static $realip = NULL; // 如果$realip不真等于NULL,返回之 if ($realip !== NULL) { return $realip; } // 如果$_SERVER有值 if (isset($_SERVER)) { // 如果$_SERVER['HTTP_X_FORWARDED_FOR']有值 // 表明客户端通过代理上网 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { // 使用explode()函数将其用','分割成数组 $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */ // 开始遍历数组 foreach ($arr AS $ip) { // 去掉首尾的空白 $ip = trim($ip); // 不是unknown就是真实上网地址,存值并退出循环 if ($ip != 'unknown') { $realip = $ip; break; } } } // $_SERVER['HTTP_X_FORWARDED_FOR']无值 且 // $_SERVER['HTTP_CLIENT_IP']有值,取其值作为真实IP elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $realip = $_SERVER['HTTP_CLIENT_IP']; } // $_SERVER['HTTP_X_FORWARDED_FOR']无值(不是用过代理上网)并且 // $_SERVER['HTTP_CLIENT_IP']也没有值 else { // 如果$_SERVER['REMOTE_ADDR']有值,取其值作为真实IP if (isset($_SERVER['REMOTE_ADDR'])) { $realip = $_SERVER['REMOTE_ADDR']; } else // 都没有值返回'0.0.0.0' { $realip = '0.0.0.0'; } } } // $_SERVER没有值 else { // 如果getenv('HTTP_X_FORWARDED_FOR')非空取其值作为真实IP if (getenv('HTTP_X_FORWARDED_FOR')) { $realip = getenv('HTTP_X_FORWARDED_FOR'); } // 如果getenv('HTTP_CLIENT_IP')非空取其值作为真实IP elseif (getenv('HTTP_CLIENT_IP')) { $realip = getenv('HTTP_CLIENT_IP'); } // 否则取getenv('REMOTE_ADDR')的值作为真实IP else { $realip = getenv('REMOTE_ADDR'); } } preg_match("/[d.]{7,15}/", $realip, $onlineip); // 此句干嘛用的?请高人指教一下。 $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0'; // 此句又是干嘛用的?请指教。 return $realip; }
最近在获取浏览客户端IP的功能的时候出现一个问题,主要的现象是这样的:一个在武汉使用北京电信的客户端的IP始终不能被我的程序访问,查了些资料修改了下,主要是对于c#获取IP的三种属性(HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR)的区分分析如下:
一、没有使用代理服务器的情况:
REMOTE_ADDR = 您的 IP
HTTP_VIA = 没数值或不显示
HTTP_X_FORWARDED_FOR = 没数值或不显示
二、使用透明代理服务器的情况:Transparent Proxies
REMOTE_ADDR = 最后一个代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 您的真实 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。
三、使用普通匿名代理服务器的情况:Anonymous Proxies
REMOTE_ADDR = 最后一个代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 代理服务器 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。
四、使用欺骗性代理服务器的情况:Distorting Proxies
REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 代理服务器 IP
HTTP_X_FORWARDED_FOR = 随机的 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
告诉了访问对象您使用了代理服务器,但编造了一个虚假的随机IP代替您的真实IP欺骗它。
五、使用高匿名代理服务器的情况:High Anonymity Proxies (Elite proxies)
REMOTE_ADDR = 代理服务器 IP
HTTP_VIA = 没数值或不显示
HTTP_X_FORWARDED_FOR = 没数值或不显示 ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
完全用代理服务器的信息替代了您的所有信息,就象您就是完全使用那台代理服务器直接访问对象。
针对以上的属性分析将代码改为:
/// <summary> /// 获取客户端Ip /// </summary> /// <returns></returns> public string GetUserIP() { string _userIP; try { if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null) { _userIP = HttpContext.Current.Request.UserHostAddress; } else { _userIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } } catch (Exception) { _userIP = "无法获取此IP"; } return _userIP; }
永久地址:
转载随意~请带上教程地址吧^^

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



The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

How to use try and catch in C requires specific code examples. In C language, there is no built-in try and catch mechanism for exception handling. However, the functionality of try and catch can be simulated by using the setjmp and longjmp functions. Below I will introduce in detail how to use these two functions for exception handling and give corresponding code examples. First, we need to understand the principles of the setjmp and longjmp functions. When the setjmp function is called, the current program will be saved.

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

PHP's preg_match() function: How to use regular expressions to match strings, specific code examples are required. Regular expressions are very powerful and flexible tools in string processing. In PHP, the preg_match() function can be used to easily perform regular string matching, thereby achieving various complex pattern matching and replacement operations. This article will introduce the usage of preg_match() function and provide specific code examples to help readers better understand and apply it. preg_match

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in
