当前位置:首页 > CN2资讯 > 正文内容

PHP 判断是否使用代理 PHP Proxy Detectorphp proxy

3天前CN2资讯


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

 

 

 

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/26622.html

    分享给朋友:

    “PHP 判断是否使用代理 PHP Proxy Detectorphp proxy” 的相关文章

    搬瓦工VPS用户必看:如何顺利更换IP地址

    板瓦工换IP是指在使用搬瓦工VPS主机时,用户因各种原因需要更改当前的IP地址。无论是因为IP被封禁,还是出于其他网络需求,了解这个过程非常重要。对于想要顺利运营自己项目的用户而言,保持IP的稳定和安全是基础,而换IP可以帮助他们解决潜在的网络问题。 在日常使用中,有些用户可能会发现他们的IP地址被...

    RackNerd IP测评:选择可靠VPS的最佳指南

    在我接触过的众多VPS服务提供商中,RackNerd以其高性价比的特点脱颖而出。作为一家位于美国的公司,RackNerd专注于为用户提供可靠的虚拟私人服务器(VPS)解决方案。在这里,我将和大家分享一些关于RackNerd的重要信息,尤其是它的IP测评,我认为这对想要选择VPS的用户来说至关重要。...

    香港云服务器:灵活选择与网络优势助力企业发展

    香港云服务器作为一种现代化的网络托管服务,逐渐成为越来越多企业和个人用户的首选。这种服务的核心就是将服务器放置在香港的数据中心,提供灵活的云计算资源。对于希望在云端运作的用户来说,了解香港云服务器的定义与特点是非常重要的。 首先,香港云服务器的产品类型多种多样,从轻量云主机到快杰云主机,再到裸金属服...

    如何有效使用WP Rocket插件提升WordPress网站性能

    WP Rocket是一个强大的高级WordPress缓存插件,它的使用对于提升网站的速度和性能起着至关重要的作用。如今,网站的加载速度对于用户体验和SEO排名都有着重要的影响。WP Rocket通过一系列功能和设置,帮助用户轻松优化自己的网站,进而增加访问量和客户满意度。 在使用WP Rocket插...

    P100 GPU价格分析及购买指南 - 如何选择性价比最高的GPU

    在如今的计算技术中,P100 GPU扮演了一个至关重要的角色。尤其是对于那些需要进行大量并行计算的任务,比如深度学习、科学模拟和数据分析,P100 GPU是一款极具吸引力的选项。作为NVIDIA推出的高性能计算单元,P100 GPU不仅具备强大的计算能力,还拥有一系列先进的技术规格,使其在行业中脱颖...

    Vultr充值优惠活动:享受高性价比云服务的最佳选择

    Vultr概述 当提到云服务,Vultr总是一个热点话题。它从2014年开始运营,迅速在行业内崭露头角。Vultr的目标是为用户提供迅速而灵活的云计算解决方案。在短短几年内,它已经覆盖了全球的多个数据中心,为用户的业务发展提供了强有力的支持。 Vultr注重用户体验,致力于让每一位客户感受到便捷与可...