PHP Proxy Detector script source

PHP script — By Script on September 18, 2009 at 12:19 pm

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:

  1. <?
  2. /**
  3. *       Proxy Detector v0.1
  4. *               copyrights by: Daantje Eeltink (me@daantje.nl)
  5. *                                               http://www.daantje.nl
  6. *
  7. *               first build: Mon Sep 18 21:43:48 CEST 2006
  8. *               last build: Tue Sep 19 10:37:12 CEST 2006
  9. *
  10. *       Description:
  11. *               This class can detect if a visitor uses a proxy server by scanning the
  12. *               headers returned by the user client. When the user uses a proxy server,
  13. *               most of the proxy servers alter the header. The header is returned to
  14. *               PHP in the array $_SERVER.
  15. *
  16. *       License:
  17. *               GPL v2 licence. (http://www.gnu.org/copyleft/gpl.txt)
  18. *
  19. *       Support:
  20. *               If you like this class and find it usefull, please donate one or two
  21. *               coins to my PayPal account me@daantje.nl
  22. *
  23. *       Todo:
  24. *               Add open proxy black list scan.
  25. */class proxy_detector {/**
  26. * CONSTRUCTOR
  27. *       Set defaults…
  28. */
  29. function proxy_detector(){
  30. $this->config = array();
  31. $this->lastLog = "";
  32.  
  33. //set default headers
  34. $this->scan_headers = array(
  35. ‘HTTP_VIA’,
  36. ‘HTTP_X_FORWARDED_FOR’,
  37. ‘HTTP_FORWARDED_FOR’,
  38. ‘HTTP_X_FORWARDED’,
  39. ‘HTTP_FORWARDED’,
  40. ‘HTTP_CLIENT_IP’,
  41. ‘HTTP_FORWARDED_FOR_IP’,
  42. ‘VIA’,
  43. ‘X_FORWARDED_FOR’,
  44. ‘FORWARDED_FOR’,
  45. ‘X_FORWARDED’,
  46. ‘FORWARDED’,
  47. ‘CLIENT_IP’,
  48. ‘FORWARDED_FOR_IP’,
  49. ‘HTTP_PROXY_CONNECTION’
  50. );
  51. }
  52.  
  53. /**
  54. * VOID setHeader( STRING $trigger )
  55. *       Set new header trigger…
  56. */
  57. function setHeader($trigger){
  58. $this->scan_headers[] = $trigger;
  59. }
  60.  
  61. /**
  62. * ARRAY $triggers = getHeaders( VOID )
  63. *       Get all triggers in one array
  64. */
  65. function getHeaders(){
  66. return $this->scan_headers;
  67. }
  68.  
  69. /**
  70. * VOID setConfig( STRING $key,  STRING $value)
  71. *       Set config line…
  72. */
  73. function setConfig($key,$value){
  74. $this->config[$key] = $value;
  75. }
  76.  
  77. /**
  78. * MIXED $config = getConfig( [STRING $key] )
  79. *       Get all config in one array, or only one config value as a string.
  80. */
  81. function getConfig($key=){
  82. if($key)
  83. return $this->config[$key];
  84. else
  85. return $this->config;
  86. }
  87.  
  88. /**
  89. * STRING $log = getLog( VOID )
  90. *       Get last logged information. Only works AFTER calling detect()!
  91. */
  92. function getLog(){
  93. return $this->lastLog;
  94. }
  95.  
  96. /**
  97. * BOOL $proxy = detect( VOID )
  98. *       Start detection and return true if a proxy server is detected…
  99. */
  100. function detect(){
  101. $log = "";
  102.  
  103. //scan all headers
  104. foreach($this->scan_headers as $i){
  105. //proxy detected? lets log…
  106. if($_SERVER[$i])
  107. $log.= "trigger $i: ".$_SERVER[$i]."\n";
  108. }
  109.  
  110. //let’s do something…
  111. if($log){
  112. $log = $this->lastLog = date("Y-m-d H:i:s")."\nDetected proxy server: ".gethostbyaddr($_SERVER[‘REMOTE_ADDR’])." ({$_SERVER['REMOTE_ADDR']})\n".$log;
  113.  
  114. //mail message
  115. if($this->getConfig(‘MAIL_ALERT_TO’))
  116. mail($this->getConfig(‘MAIL_ALERT_TO’),"Proxy detected at {$_SERVER['REQUEST_URI']}",$log);
  117.  
  118. //write to file
  119. $f = $this->getConfig(‘LOG_FILE’);
  120. if($f){
  121. if(is_writable($f)){
  122. $fp = fopen($f,‘a’);
  123. fwrite($fp,"$log\n");
  124. fclose($fp);
  125. }else{
  126. 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…");
  127. }
  128. }
  129.  
  130. //done
  131. return true;
  132. }
  133.  
  134. //nope, no proxy was logged…
  135. return false;
  136. }
  137. }
  138.  
  139. ?>
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

Random Articles

0 Comments

You can be the first one to leave a comment.

Leave a Comment


Tags: , , , , , , ,