Posted By

MMDeveloper on 12/29/09


Tagged

php ip user block access agent host


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

alyssonweb


PHP Based Access Blocking Version 2.0


 / Published in: PHP
 

URL: http://skyward.nefec.org

This is a new version of a previous snippet I wrote. This version is smaller, more efficient, and allows the use of regular expressions. To use:

$blocker = new blocker();
if ($blocker->isBlocked === true) {
    //blocked user, do whatever
} else {}

OR if you want to test IF a certain IP or hostname or user agent WOULD be blocked:

$blocker = new blocker();
list($isBlocked, $reasonsBlocked) = $blocker->testIfBlocked([userIP], [userAgent], [userHostname]);
if ($isBlocked === true) {
    print_r($reasonsBlocked);
} else {}

The 3 arguments are optional, but if specified, must be specified in that order. Any unspecified arguments will default to the values of the user executing the call. For example, if you do not specify a user agent and host name, and you're the one executing the test, it will use YOUR user agent and host name. An array of reasons why the test said you failed will be returned (along with a boolean true/false as to if the criteria would be blocked or not).

  1. <?php
  2.  
  3. /**
  4.  * This is the Security/Blocking class
  5.  * @package Skyward_Landing_Page
  6.  * @subpackage Tier_1_Security
  7.  * @filesource
  8.  * @author Matt Ford
  9.  * @version 1.0
  10.  */
  11.  
  12. //if (!defined("parentFile")) {die("Direct Access Not Allowed");} else {}
  13.  
  14. class blocker {
  15.  
  16. /**
  17. * array if IP regexp's
  18. * @var array
  19. */
  20. private $ips = array(
  21. "^10\.40\.",
  22. "\.3\.105$",
  23. "^195\.178\.177"
  24. );
  25.  
  26. /**
  27. * array if User Agent regexp's
  28. * @var array
  29. */
  30. private $userAgents = array(
  31.  
  32. );
  33.  
  34. /**
  35. * array if Hostname regexp's
  36. * @var array
  37. */
  38. private $hostNames = array(
  39.  
  40. );
  41.  
  42. /**
  43. * array if reasons the user is blocked
  44. * @var array
  45. */
  46. public $reasonsBlocked = array();
  47.  
  48. /**
  49. * true/false if user is blocked
  50. * @var boolean
  51. */
  52. public $isBlocked = false;
  53.  
  54. public function __construct() {
  55. list($isBlocked, $reasonsBlocked) = $this->testIfBlocked();
  56. $this->isBlocked = $isBlocked;
  57. $this->reasonsBlocked = $reasonsBlocked;
  58. }
  59.  
  60. public function testIfBlocked($ip = "", $ua = "", $host = "") {
  61.  
  62. $check_ip = ($ip == "") ? $_SERVER["REMOTE_ADDR"] : $ip;
  63. $check_ua = ($ua == "") ? $_SERVER["HTTP_USER_AGENT"] : $ua;
  64. $check_host = ($host == "") ? gethostbyaddr($_SERVER["REMOTE_ADDR"]) : $host;
  65. $isBlocked = false;
  66. $reasonsBlocked = array();
  67.  
  68. foreach ($this->ips as $ip) {
  69. if (preg_match("/" . $ip . "/", $check_ip, $tempMatches) > 0) {
  70. $reasonsBlocked[] = $this->registerMatch("IP", $ip, $check_ip, $tempMatches);
  71. $isBlocked = true;
  72. } else {}
  73. }
  74.  
  75. foreach ($this->userAgents as $ua) {
  76. if (preg_match("/" . $ua . "/", $check_ua, $tempMatches) > 0) {
  77. $reasonsBlocked[] = $this->registerMatch("User Agent", $ua, $check_ua, $tempMatches);
  78. $isBlocked = true;
  79. } else {}
  80. }
  81.  
  82. foreach ($this->hostNames as $hn) {
  83. if (preg_match("/" . $hn . "/", $check_host, $tempMatches) > 0) {
  84. $reasonsBlocked[] = $this->registerMatch("Host Name", $hn, $check_host, $tempMatches);
  85. $isBlocked = true;
  86. } else {}
  87. }
  88.  
  89. return array ($isBlocked, $reasonsBlocked);
  90. }
  91.  
  92. /**
  93. * This method registers the block/match arguments into the array of reasons the user was blocked
  94. * @access public
  95. * @author Matt Ford
  96. * @category Blocking
  97. * @param string $type match type, such as IP match, User Agent Match, etc
  98. * @param string $rule the RegExp rule responsible for the match
  99. * @param string $source the source string the RegExp rule matched against successfully
  100. * @param array $matches the matches output from the preg_match function call
  101. */
  102. private function registerMatch($type, $rule, $source, $matches) {
  103. return array (
  104. "type" => $type,
  105. "rule" => $rule,
  106. "source" => $source,
  107. "matches" => $matches
  108. );
  109. }
  110. }
  111. ?>

Report this snippet  

You need to login to post a comment.