当前位置:首页 > 开发 > 编程语言 > PHP > 正文

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

发表于: 2015-07-19   作者:天梯梦   来源:转载   浏览:
摘要: 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

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 (me@daantje.nl)
*						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 me@daantje.nl
*
*	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: me@daantje.nl
*
*	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','me@daantje.nl');
// $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> - &copy;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");
?>

 

原文:http://www.roscripts.com/PHP_Proxy_Detector-75.html

DEMO: http://sources.ikeepstudying.com/proxy/  (开启代理,网页打开有可能比正常的要慢!)

 

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.

原文:http://www.shroomery.org/ythan/proxyblock.php

 

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');
}

 

原文:https://forums.digitalpoint.com/threads/php-to-detect-users-accessing-site-via-proxy-server.58964/

 

转自: PHP 判断是否使用代理 PHP Proxy Detector

 

 

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
select count(people) c from people where people='乐乐' 上面这条sql就是原理 php利用代码 <?p
前提 需要判断远程URL是否有效,远程url包括远程图片,网页,视频等等元素 解决办法: 使用PHP解决
如何判断PHP空间是否支持curl, gzip等功能 在使用PHP时,我们需要将自己写好的php文件上传到已申请
前提 需要判断远程URL是否有效,远程url包括远程图片,网页,视频等等元素 解决办法: 一、使用PHP
在php中使用Xajax能够即时与数据库发生交互‘ 带给用户更好的体验 主要的应用有网页的即时、不刷新
代理(Proxy) 如果想在运行时生成新的对象,并且这个对象的类型是全新的,是现有系统中没有的。就可
声明:本系列博客参考资料《大话设计模式》,作者程杰。 代理模式为其他对象提供一种代理以控制对这
php的全局变量和其余编程语言是不同的,在大多数的编程语言中,全局变量在其下的函数、类中自动生效
今天学习了ExtJS的Proxy代理,理解Proxy代理用了一点时间,现在总算是有点眉目了,跟代理相关的几个
Java动态代理一——动态类Proxy的使用 1.什么是动态代理? 答:动态代理可以提供对另一个对象的访问
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号