簡體   English   中英

以編程方式將ip地址從mysql表添加到.htaccess文件並阻止用戶

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

大家好,我們的編程朋友們,根據這個問題的主題,我在整個站點上搜索了有關該主題的相關問題。 但是我沒有找到類似於從數據庫表讀取並將值解析為.htaccess文件的東西。

任務是這樣的:

如果php從特定的IP地址記錄了3次失敗的登錄嘗試,則應調用.htaccess來阻止該用戶的訪問。

此任務可在此站點上找到。 但是下面的幾行不可用:

1)每次有人嘗試僅訪問此文件夾中的所有文件時,均應調用.htaccess。 2).htaccess應該讀取一個mysql表,並將傳入的ip與mysql表中的ips列表進行比較。 3)如果匹配,.htaccess應將用戶重定向到自由進入位置。

請注意,我可以將php與mysql select語句一起使用,以檢查文件夾內文件的位置。 但是我不希望出現這種情況。 我寧願使用.htaccess文件。

在這里,.htaccess文件將不會記錄IP地址,而只會檢查該IP地址是否已在表中。

例如,下面的偽代碼:

.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

請不要介意我的偽代碼...。盡管它看起來很愚蠢和幼稚。 那是個概念,但是,我真的不知道如何讀寫.htaccess文件,也不知道.htaccess編程使用哪種編程語言。

請任何幫助將不勝感激...謝謝。

我在一個網站上找到了它,但這似乎正是您想要的。

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

  }

}

在.htaccess中

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

資源

這里看看已接受的答案

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

您可以使用您的php代碼更新blacklist.txt。恕我直言,這要安全得多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM