如何将特定标签&lt之前使用fwrite; /文件>如何将、标签、文件、lt

2023-09-02 11:25:53 作者:开着航母去钓鱼

我想提出一个脚本在PHP将使用fwrite和它的广告后的< /文件>标记,它没有得到包括在内。的主要目的是记录每个IP在此.htaccess文件以没有访问这个特定的文件。这是我的code:(搜索约3+小时的谷歌和放大器; php.net)。我曾经想过,如果有一种方法从该文件的.htaccess里的字与LT读/ file>是之前添加$ IP。或者一些其他的方式来获得的&lt里面的$ b​​adpersonip;文件forum.php *

I am making a script in which php will fwrite and it ads it after the </file> tag and it doesnt get included. The main purpose is to log each IP in this .htaccess file in order to not have access to this specific file. Here is my code: (Searching about 3+ hours on google & php.net). I have thought if there is a way to read from the file .htaccess where the "word" </file> is and before it add the $ip. Or some other way to get the $badpersonip inside the <file forum.php *(I can not use a database so it needs to be done solely from PHP and .htaccess)

<?php

$badpersonip = $_SERVER['REMOTE_ADDR'];

echo "You have been banned $badpersonip , dont spam!! <br> ";
$ip = "deny from $badpersonip n";
$banip = '.htaccess';
$fp = fopen($banip, "a");
$write = fputs($fp, $ip);

?>

另外这里是我的.htaccess code:

Also here is my .htaccess code:

<files forum.php>
Order Allow,Deny
Allow from all

deny from 127.0.2.1
deny from 127.1.2.1

</files>
deny from 127.0.0.3

正如你看到它的广告新的IP,但在探底后的文件标记已关闭。 :(

As you see it ads the new IP but on the bottom AFTER the files tag has closed. :(

感谢您的帮助真的AP preciated。

Thank you for your help really appreciated.

推荐答案

。如果不是使用的fwrite()你读了整个事情变成一个字符串的 的file_get_contents() ,你可以很容易做到 str_replace()函数来替换现有的&LT; /文件&GT; 新线和&LT; /文件&GT;

If instead of using fwrite() you read the whole thing into a string with file_get_contents(), you can easily do str_replace() to replace the existing </files> with the new line and </files>

// Read the while file into a string $htaccess
$htaccess = file_get_contents('.htaccess');
// Stick the new IP just before the closing </files>
$new_htaccess = str_replace('</files>', "deny from $badpersonipn</files>", $htaccess);
// And write the new string back to the file
file_put_contents('.htaccess', $new_htaccess);

这是不建议,如果你期望的文件变得非常大,但是对于几十或几百个IP地址,应该很好地工作。这不会完全一样的,如果你有一个以上的&LT; /文件&GT; 在.htaccess文件。这就需要更仔细的分析,找到正确的结束标记。

This isn't recommended if you expect the file to become very large, but for a few dozen or few hundred IPs it should work fine. This won't work exactly as is if you have more than one </files> in that .htaccess file. That would require more careful parsing to find the correct closing tag.

如果preserving空白(如果你有以前的缩进&LT; /文件&GT; )对你很重要,你考虑使用 preg_replace()代替简单的 str_replace()函数

If preserving whitespace (if you have indentation before </files>) is important to you, you look into using preg_replace() in lieu of the simpler str_replace().

另一种方法,这将是使用文件()读的.htaccess到其线阵列,找到包含行&LT ; /文件&GT; 并插入一个新的数组元素之前,再加入线重新走到一起,并​​将其写入文件

Another method to this would be to use file() to read the .htaccess into an array of its lines, locate the line containing </files> and insert a new array element before it then join the lines back together and write it to the file.