PHP code to block Access to Blacklisted IPs

99 Views Asked by At

I am have written this code to block access to IPs in the blacklist and allow access to whitelisted IPs on my website but the site seems to be having a problem with retrieving IPs from the whitelisted IPs file. I don't really know what is wrong here. Any help with what to do to fix this please.

This is the code

class IpBlockList {
private $statusid = array('negative' => - 1, 'neutral' => 0, 'positive' => 1);
private $whitelist = array();
private $blacklist = array();
private $message = NULL;
private $status = NULL;
public function __construct($whitelistfile = './security/whitelist.dat', $blacklistfile = './security/blacklist.dat') {
    $this->whitelistfile = $whitelistfile;
    $this->blacklistfile = $blacklistfile;
    $this->whitelist = new IpList($whitelistfile);
    $this->blacklist = new IpList($blacklistfile);
}
1

There are 1 best solutions below

0
Romeo Dabok On BEST ANSWER

Just add these with the rest of the initial variables. Would greatly help you if that object gets complex (and I believe it will) :

private $whitelistfile;
private $blacklistfile;

So your code should look like this:

class IpBlockList {
   private $statusid = array('negative' => - 1, 'neutral' => 0, 'positive' => 1);
   private $whitelist = array();
   private $blacklist = array();
   private $whitelistfile;
   private $blacklistfile;
   private $message = NULL;
   private $status = NULL;
   public function __construct($whitelistfile = './security/whitelist.dat', $blacklistfile = './security/blacklist.dat') {
      $this->whitelistfile = $whitelistfile;
      $this->blacklistfile = $blacklistfile;
      $this->whitelist = new IpList($whitelistfile);
      $this->blacklist = new IpList($blacklistfile);
   }
}

However, without any error log to look at, I can't be sure if that's the only thing that's wrong. The only thing I can see is wrong is that you didn't put the last curly bracket (}) to close the object block