简体   繁体   English

以编程方式将ip地址从mysql表添加到.htaccess文件并阻止用户

[英]programmatically add ip address to .htaccess file from mysql table and block user

Hi good programming friends of ours, based on the subject of this question, I have searched the entire site for related questions on the subject. 大家好,我们的编程朋友们,根据这个问题的主题,我在整个站点上搜索了有关该主题的相关问题。 But I have not found something similarly to reading from a db table and parsing values to .htaccess file. 但是我没有找到类似于从数据库表读取并将值解析为.htaccess文件的东西。

The quest is this: 任务是这样的:

If php records 3 failed login attempts from a particular ip address, .htaccess should be invoked to block access from that user. 如果php从特定的IP地址记录了3次失败的登录尝试,则应调用.htaccess来阻止该用户的访问。

This quest is available on this site. 此任务可在此站点上找到。 But what is not available is the next lines below: 但是下面的几行不可用:

1) .htaccess should be invoked each time a person tries to access all the files only in this folder . 1)每次有人尝试仅访问此文件夹中的所有文件时,均应调用.htaccess。 2) .htaccess should read a table mysql and compare the incoming ip with a list of ips in mysql table. 2).htaccess应该读取一个mysql表,并将传入的ip与mysql表中的ips列表进行比较。 3) if there is a match, .htaccess should redirect the user to a free-entry location. 3)如果匹配,.htaccess应将用户重定向到自由进入位置。

Note that, I can use php with mysql select statement to check this on the file inside folder. 请注意,我可以将php与mysql select语句一起使用,以检查文件夹内文件的位置。 But I do not want this scenario. 但是我不希望出现这种情况。 I rather want to use .htaccess file. 我宁愿使用.htaccess文件。

Here, .htaccess file will not record ip address, but will only check whether such ip is already in the table. 在这里,.htaccess文件将不会记录IP地址,而只会检查该IP地址是否已在表中。

For instance - the psudo-code below: 例如,下面的伪代码:

.htaccess file enabled = true
create a temporary variable ($ip) in .htaccess file
On user access to any .php pages in /test/ folder,
retrieve user's ip and temporarily store in $ip variable.

open connection on .php page
load login table having ip match as $ip.
if found then redirect
else continue to loading page
end if
close connection
end on
destroy the variable

please do not mind my psudo-code.... though it may look stupid and childish. 请不要介意我的伪代码...。尽管它看起来很愚蠢和幼稚。 That's the concept but, I do not really know how to write to or read from .htaccess filee, nor what programming language is used on .htaccess programming. 那是个概念,但是,我真的不知道如何读写.htaccess文件,也不知道.htaccess编程使用哪种编程语言。

Please any help will be appreiated... Thank you. 请任何帮助将不胜感激...谢谢。

I found this on a site, but it seems to be exactly what you're looking for. 我在一个网站上找到了它,但这似乎正是您想要的。

<?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>';

  }

}

In .htaccess 在.htaccess中

<FilesMatch 403.shtml>
Order Allow,Deny
Allow From All
</FilesMatch>

Source 资源

Take a look at the accepted answer here 这里看看已接受的答案

RewriteMap access txt:/path/to/blacklist.txt

you can use your php code to update the blacklist.txt It's much safer IMHO 您可以使用您的php代码更新blacklist.txt。恕我直言,这要安全得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM