首页 > 编程语言 >PHP 判断是否使用代理 PHP Proxy Detector

PHP 判断是否使用代理 PHP Proxy Detector

时间:2023-03-29 16:08:14浏览次数:43  
标签:log ... HTTP FORWARDED SERVER Proxy Detector PHP proxy


1. php 类

I found this class looking for something else actually but I remembered I needed some while ago something similar and I never found one. I'm sure it will help a lot of developers who try to detect click frauds or something else. The class will scan the headers of the visitor which (in most cases when using proxies) is altered by the server. I will keep the code intact so visitors can give credits to the author :) We will have to create 4 files. Let's start with the first one called "proxy_detector.class.php" which is our little core with the class in clause. Copy paste this code and save it:

<?
/**
*	Proxy Detector v0.1
*		copyrights by: Daantje Eeltink ([email protected])
*						http://www.daantje.nl
*
*		first build: Mon Sep 18 21:43:48 CEST 2006
*		last build: Tue Sep 19 10:37:12 CEST 2006
*
*	Description:
*		This class can detect if a visitor uses a proxy server by scanning the
*		headers returned by the user client. When the user uses a proxy server,
*		most of the proxy servers alter the header. The header is returned to
*		PHP in the array $_SERVER.
*
*	License:
*		GPL v2 licence. (http://www.gnu.org/copyleft/gpl.txt)
*
*	Support:
*		If you like this class and find it usefull, please donate one or two
*		coins to my PayPal account [email protected]
*
*	Todo:
*		Add open proxy black list scan.
*/

class proxy_detector {

	/**
	* CONSTRUCTOR
	*	Set defaults...
	*/
	function proxy_detector(){
		$this->config = array();
		$this->lastLog = "";

        //set default headers
		$this->scan_headers = array(
			'HTTP_VIA',
			'HTTP_X_FORWARDED_FOR',
			'HTTP_FORWARDED_FOR',
			'HTTP_X_FORWARDED',
			'HTTP_FORWARDED',
			'HTTP_CLIENT_IP',
			'HTTP_FORWARDED_FOR_IP',
			'VIA',
			'X_FORWARDED_FOR',
			'FORWARDED_FOR',
			'X_FORWARDED',
			'FORWARDED',
			'CLIENT_IP',
			'FORWARDED_FOR_IP',
			'HTTP_PROXY_CONNECTION'
		);
	}


	/**
	* VOID setHeader( STRING $trigger )
	*	Set new header trigger...
	*/
	function setHeader($trigger){
		$this->scan_headers[] = $trigger;
	}


	/**
	* ARRAY $triggers = getHeaders( VOID )
	*	Get all triggers in one array
	*/
	function getHeaders(){
    	return $this->scan_headers;
	}


	/**
	* VOID setConfig( STRING $key,  STRING $value)
	*	Set config line...
	*/
	function setConfig($key,$value){
		$this->config[$key] = $value;
	}


	/**
	* MIXED $config = getConfig( [STRING $key] )
	*	Get all config in one array, or only one config value as a string.
	*/
	function getConfig($key=''){
    	if($key)
    		return $this->config[$key];
    	else
    		return $this->config;
	}


	/**
	* STRING $log = getLog( VOID )
	*	Get last logged information. Only works AFTER calling detect()!
	*/
	function getLog(){
		return $this->lastLog;
	}


	/**
	* BOOL $proxy = detect( VOID )
	*	Start detection and return true if a proxy server is detected...
	*/
	function detect(){
		$log = "";

		//scan all headers
		foreach($this->scan_headers as $i){
			//proxy detected? lets log...
			if($_SERVER[$i])
				$log.= "trigger $i: ".$_SERVER[$i]."n";
		}

    	//let's do something...
		if($log){
			$log = $this->lastLog = date("Y-m-d H:i:s")."nDetected proxy server: ".gethostbyaddr($_SERVER['REMOTE_ADDR'])." ({$_SERVER['REMOTE_ADDR']})n".$log;

			//mail message
            if($this->getConfig('MAIL_ALERT_TO'))
				mail($this->getConfig('MAIL_ALERT_TO'),"Proxy detected at {$_SERVER['REQUEST_URI']}",$log);

			//write to file
			$f = $this->getConfig('LOG_FILE');
            if($f){
				if(is_writable($f)){
					$fp = fopen($f,'a');
					fwrite($fp,"$logn");
					fclose($fp);
            	}else{
					die("<strong>Fatal Error:</strong> Couldn't write to file: '<strong>$f</strong>'<br>Please check if the path exists and is writable for the webserver or php...");
            	}
            }

			//done
			return true;
		}

		//nope, no proxy was logged...
		return false;
	}
}

?>

 

Browsing the code you will notice that it uses a log file to store the data so we will have to create one called "proxy_detector.log". Don't forget to give it the proper permission on the server (CHMOD it to make it writable). Ok so we already have 2 files. Let's go ahead and create a new one called "proxy_detector.inc.php". This one will initiate our class for our future use and do what we want it to do so I suggest you to edit it to suit your needs. Copy paste this code and save it:

<?
/**
* Proxy Detector v0.1
*	Implementation example.
*
*	Mon Sep 18 23:29:47 CEST 2006
*	by: [email protected]
*
*	Documentation:
*		I use this file as an include at the top of some php files
*		to block proxy users from the scripts that included this file.
*
*		This file is only an example on how to implement the detector class.
*		But it could be usefull as is...
*
*		Check the remarks in the class for more documentation.
*/

//include detector class, assuming it's in the same directory as this file...
include_once(dirname(__FILE__)."/proxy_detector.class.php");

//init class
$proxy = new proxy_detector();

//set optional extra triggers, no need to... I think I've got all of them covered in the class...
// $proxy->setTrigger('HTTP_SOME_HEADER_1');
// $proxy->setTrigger('HTTP_SOME_HEADER_2');

//set optional config
// $proxy->setConfig('MAIL_ALERT_TO','[email protected]');
// $proxy->setConfig('LOG_FILE','/home/daantje/public_html/proxy/proxy_detector.log');

//start detect
if($proxy->detect()){

    //returned true, lets die...
	echo "<h1>Proxy detected</h1>";
	echo "Please disable your proxy server in your browser preferences or internet settings, and try again.<br><br>";

	//parse logged info
	echo nl2br($proxy->getLog());

    //some credits...
	echo "<hr><strong>proxy detector v0.1</strong> - ©2006 <a href="http://www.daantje.nl" target="_blank">daantje.nl</a>";

	//and do nothing anymore! (but not in my example)
	//exit();
}

//else, proceed as normal, put your code here...
?>

 

The 4th file is optional but I like to store everything in folders so I'll create an index that, once called in your pages, will trigger this class and do what you've set it to do in "proxy_detector.inc.php". Copy paste this code into this file that we will call "index.php" and we're done:

<?
include_once("proxy_detector.inc.php");
?>

 

 

2. 利用第三方

http://www.shroomery.org/ythan/proxycheck.php?ip=127.0.0.1

The response is a single character and will contain one of three values: Y if it's a proxy, N if it isn't, or X if there's an error.

 

3. 其他

if (
      $_SERVER['HTTP_X_FORWARDED_FOR']
   || $_SERVER['HTTP_X_FORWARDED']
   || $_SERVER['HTTP_FORWARDED_FOR']
   || $_SERVER['HTTP_VIA']
   || in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554))
   || @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30))
{
    exit('Proxy detected');
}

 

 

 

标签:log,...,HTTP,FORWARDED,SERVER,Proxy,Detector,PHP,proxy
From: https://blog.51cto.com/u_8895844/6157196

相关文章

  • deepin系统安装php8
    deepin系统安装php8一、deepin系统与php的版本1.deepin系统的版本 --社区版(20.8)2.php的版本 --PHP8.1.11(cli)二、下载php的源码包1.官网下载地址https://www......
  • PHP 获取当前完整URL路径
    不包含#$http_type=((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO'......
  • 【Visual Leak Detector】配置项 StackWalkMethod
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项StackWalkMethod的使用方法。同系列文章目录可见《内存泄漏检测工具》目录......
  • ThinkPHP框架:更新个别字段的值setField、setInc、setDec的用法
    ThinkPHP有三个更新个别字段的值的函数,分别为setField、setInc、setDec。setInc():将数字字段值增加setDec():将数字字段值减少setField,根据条件更新一个或多个字段的值......
  • php 安装扩展 event
    本地环境php8.1,然后我想安装event扩展,找了找资料,直接一句话sudopeclinstallevent然后执行的过程中提示这些configure.ac:165:thetoplevelEnableinternal......
  • 一文剖析:LVS/Nginx/HAProxy原理及应用场景
    负载均衡已经发展成为网络架构中的基础核心组件,消除了服务器单点故障,可以进行请求流量分流,提升冗余,保证服务器的稳定性。在开源的软件负载均衡中,应用最为广泛的有LVS、Nginx......
  • 【Visual Leak Detector】配置项 ReportEncoding
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项ReportEncoding的使用方法。同系列文章目录可见《内存泄漏检测工具》目录......
  • 【Visual Leak Detector】配置项 MaxTraceFrames
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项MaxTraceFrames的使用方法。同系列文章目录可见《内存泄漏检测工具》目录目......
  • 【Visual Leak Detector】配置项 MaxDataDump
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项MaxDataDump的使用方法。同系列文章目录可见《内存泄漏检测工具》目录目录......
  • 【Visual Leak Detector】配置项 AggregateDuplicates
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍VLD配置文件中配置项AggregateDuplicates的使用方法。目录说明1.配置文件使用说明2.设置是否显......