从php块文件重定向白名单IP?

从php块文件重定向白名单IP?,php,.htaccess,Php,.htaccess,我使用下面的php文件来禁止不允许的Ip地址,并阻止他们访问某些文件。 这是php文件 <?php // Get the IP address of the visitor so we can work with it later. $ip = $_SERVER['REMOTE_ADDR']; // This is where we pull the file and location of the htaccess file. If it's in // the same dire

我使用下面的php文件来禁止不允许的Ip地址,并阻止他们访问某些文件。 这是php文件

<?php


// Get the IP address of the visitor so we can work with it later.
$ip = $_SERVER['REMOTE_ADDR'];

// This is where we pull the file and location of the htaccess file. If it's in
// the same directory as this php file, just leave it as is.
$htaccess = '.htaccess';

// This pulls the current contents of your htaccess file so we can search it later.
$contents = file_get_contents($htaccess, TRUE) 
          OR exit('Unable to open .htaccess');

// Lets search the htaccess file to see if there is already a ban in place.
$exists = !stripos($contents, 'deny from ' . $ip . "\n") 
          OR exit('Already banned, nothing to do here.');

// Here we just pull some details we can use later.
$date   = date('Y-m-d H:i:s');
$uri    = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
$agent  = htmlspecialchars($_SERVER['HTTP_USER_AGENT'], ENT_QUOTES);
$agent  = str_replace(array("\n", "\r"), '', $agent);

// If you would like to be emailed everytime a ban happens, put your email
// INSIDE the quotes below. (e.g. 'my@email.com')
$email = '';

// This is where we can whitelist IP's so they can never be banned. Simply remove 
// the //  from the front of one of the example IP addresses below and add the 
// address you wish to whitelist. Make sure that you leave the single quotes (') 
// intact and the comma at the end. Adding a person to the whitelist AFTER they 
// have been banned will NOT remove them. You must open the htaccess file and 
// locate their ban by hand and remove it.
$whitelist = array(
  // '123.123.123.123',
  // '123.123.123.123',
  // '123.123.123.123',
);


// This section prevents people from being sent to this script by mistake
// via a link, image, or other referer source. If you don't want to check
// the referer, you can remove the following line. Make sure you also
// remove the ending } at the very end of this script.
if (empty($_SERVER['HTTP_REFERER'])) {

// This section will write the IP address to the htaccess file and in turn
// ban the address. It will however check the whitelist above to see if
// should be banned.
  if (in_array($ip, $whitelist)) {

    // User is in whitelist, print a message and end script.
    echo "Hello user! Because your IP address ({$ip}) is in our whitelist,
    you were not banned for attempting to visit this page. End of line.";

  } else {

    // User is NOT in whitelist - we need to ban em...
    $ban =  "\n# The IP below was banned on $date for trying to access {$uri}\n";
    $ban .= "# Agent: {$agent}\n";
    $ban .= "Deny from {$ip}\n";

    file_put_contents($htaccess, $ban, FILE_APPEND) 
          OR exit('Cannot append rule to .htaccess');

    // Send email if address is specified
    if (!empty($email)) {
      $message = "IP Address: {$ip}\n";
      $message .= "Date/Time: {$date}\n";
      $message .= "User Agent: {$agent}\n";
      $message .= "URL: {$uri}";

      mail($email, 'Website Auto Ban: ' . $ip, $message);
    }

    // Send 403 header to browser and print HTML page
    header('HTTP/1.1 403 Forbidden', TRUE);
    echo '<html><head><title>Error 403 - Banned</title></head><body>
    <center><h1>Error 403 - Forbidden</h1>Hello user, you have been 
    banned from accessing our site. If you feel this ban was a mistake, 
    please contact the website administrator to have it removed.<br />
    <em>IP Address: '.$ip.'</em></center></body></html>';

  }

}
而不是:

// User is in whitelist, print a message and end script.
echo "Hello user! Because your IP address ({$ip}) is in our whitelist,
you were not banned for attempting to visit this page. End of line.";
只要做:

header("Location: /place-to-redirect");
exit();
而不是:

// User is in whitelist, print a message and end script.
echo "Hello user! Because your IP address ({$ip}) is in our whitelist,
you were not banned for attempting to visit this page. End of line.";
只要做:

header("Location: /place-to-redirect");
exit();

为什么不简单一点呢

$allowed = array(
    'xxx.xxx.xxx.xxx',
);
if ((in_array($_SERVER['REMOTE_ADDR'], $allowed))==false) { header("Location: http://www.domain.com/blocked"); }else{

    echo "You're in!";

}

为什么不简单一点呢

$allowed = array(
    'xxx.xxx.xxx.xxx',
);
if ((in_array($_SERVER['REMOTE_ADDR'], $allowed))==false) { header("Location: http://www.domain.com/blocked"); }else{

    echo "You're in!";

}

禁止知识产权是毫无意义的。很容易改变IP,1个IP可以是很多人。@Dagon我不认为这是毫无意义的,但谢谢你的评论。你认为你取得了什么成就?我可以绕过你设置的任何IP阻塞。当然,使用这样的东西更有效、更可维护——实际上更改.htaccess文件(我通常不允许Web服务器这么做):禁止IP是毫无意义的。很容易改变IP,1个IP可以是很多人。@Dagon我不认为这是毫无意义的,但谢谢你的评论。你认为你取得了什么成就?我可以绕过您设置的任何IP阻塞。当然,使用类似这样的方法更有效、更易于维护。实际上更改.htaccess文件(我通常不允许Web服务器使用该文件):如果这样做,我会得到重定向loop@laviku然后您可以尝试只包含该文件,例如:
include(“重定向的位置”)
由于无论IP是什么,您都要将所有内容路由到
block.php
block.php
将需要知道如何为被列入白名单的人提供页面服务。我猜外部重定向不起作用。您的另一个选择是使用mod_rewrite为您阻止IP。如果我这样做,我会得到一个重定向循环@然后您可以尝试只包含该文件,比如:
include(“要重定向的位置”)
由于无论IP是什么,您都要将所有内容路由到
block.php
block.php
将需要知道如何为被列入白名单的人提供页面。我猜外部重定向不起作用。您的另一个选择是使用mod_rewrite为您阻止IP