Posted By

MMDeveloper on 10/01/08


Tagged

php blocking security access


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Scooter
bryandease


php based access blocking


 / Published in: PHP
 

URL: http://www.mechanicmatt.com

Using the code below, you simply include the file in your application like this:

$redirect = true; include("/www/htdocs/checkBlocked.php");

if $redirect is true, then (if the visitor is blocked), it will automatically redirect them to the URL specified in the script below. If it does not, then it simply returns a boolean true/false if the user is blocked or not (for custom processing) in a variabled called '$blocker_isBlocked'.

To add IP's and/or domains to the blacklist, just add them to the respective arrays where the commented lines are.

  1. <?php
  2.  
  3. $blocker_isBlocked_redirect = "http://www.mechanicmatt.com/Access+Denied-p71.html";
  4. $blocker_isBlocked_return = "||{{{error}}}||";
  5. $blocker_isBlocked_err_msg = "You have been temporarily blocked from accessing this content.";
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. //blocking class
  13. class blocker {
  14.  
  15. var $ips = NULL;
  16. var $domains = NULL;
  17. var $currentEnv = NULL;
  18. var $blocked = NULL;
  19. var $redirect = NULL;
  20.  
  21. function blocker($env, $redirect = true) {
  22. $this->ips = array (
  23. //"67.191.103.115"
  24. );
  25.  
  26. $this->domains = array (
  27. //"nefec.org"
  28. );
  29.  
  30. $this->blocked = false;
  31.  
  32. if (is_null($redirect)) {
  33. $this->redirect = true;
  34. }
  35. else {
  36. $this->redirect = $redirect;
  37. }
  38.  
  39. $this->currentEnv = $env;
  40. }
  41.  
  42. function isBlocked() {
  43. global $blocker_isBlocked_redirect;
  44.  
  45. $this->checkIP();
  46. $this->checkHostname();
  47.  
  48. if ($this->blocked == true && $this->redirect == true) {
  49. header("Location: " . $blocker_isBlocked_redirect);
  50. }
  51. else {
  52. return $this->blocked;
  53. }
  54. }
  55.  
  56. function checkIP() {
  57. if (in_array($this->currentEnv["REMOTE_ADDR"], $this->ips)) { //check for exact match
  58. $this->blocked = true;
  59. }
  60. else { //exhaustive partial match
  61. foreach ($this->ips as $ip) {
  62. if (ereg("^(".$ip.")", $this->currentEnv["REMOTE_ADDR"])) {
  63. $this->blocked = true;
  64. }
  65. else {
  66.  
  67. }
  68. }
  69. }
  70. }
  71.  
  72. function checkHostname() {
  73. $userHostName = gethostbyaddr($this->currentEnv["REMOTE_ADDR"]);
  74. foreach ($this->domains as $k => $v) {
  75. if (eregi($v, $userHostName)) {
  76. $this->blocked = true;
  77. } else {}
  78. }
  79. }
  80. }
  81.  
  82. $blocker = new blocker($_SERVER, $redirect);
  83. $blocker_isBlocked = $blocker->isBlocked();
  84. ?>

Report this snippet  

You need to login to post a comment.