问题创造一个充满活力的.htaccess禁令 [英] Problems creating a dynamic .htaccess ban
问题描述
我想实现我自己用的一个小应用程序读取发送到从联系表我的数据库中的数据,我希望能够禁止那些不受欢迎的接触形式的内容,用户,不久。所以,我,我有充分的用户IP,发送的形式。但是,它只能保存来自数据库每一次否定我点击禁止按钮,我不知道为什么。这里是全code:
< PHP
如果(使用isset($ _ POST [提交])){
//读取,而文件转换成字符串$ htaccess的
$ htaccess的=的file_get_contents('的.htaccess');
//坚持新的IP刚刚结束&lt前; /文件>
$ new_htaccess = str_replace函数('允许从所有',从拒绝,$ unwanteduser\ nallow从所有,$ htaccess的);
//并写入新的字符串返回到该文件
file_put_contents('的.htaccess',$ new_htaccess);
}
?>
<!DOCTYPE HTML>
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<冠军>Yhteydenottopyynnöt< /标题>
<风格>
身体{宽度:100%;}
TR:第n个孩子(甚至){背景:#ccc; }
< /风格>
< /头>
<身体GT;
< PHP
$ CON =的mysql_connect(localhost,则用户,通行证);
如果(!$ CON)
{
死亡(无法连接:mysql_error());
}
mysql_select_db(DB,$ CON);
$结果= mysql_query(SELECT * FROM wp_contactform);
$ F =的fopen(htaccess的,A +);
$ IP = $行['IP'];
FWRITE($ IP,$ F);
fclose函数($ F);
回声<表格边框='1'>
&其中; TR>
百分位风格=宽度:5%;'> ID< /第i个
百分位风格='宽度:10%;'> NIMI< /第i个
百分位风格='宽度:10%;'> Puhelin< /第i个
百分位风格='宽度:10%;'> Sposti< /第i个
百分位风格='宽度:40%;> Viesti< /第i个
百分位风格='宽度:10%;'> P&安培; AUML; 4&安培; AUML;< /第i个
百分位风格= '10%'> IP< /第i个
百分位风格='5%'>禁止< /第i个
&所述; / TR>中;
$ i = 0;
而($行= mysql_fetch_array($结果))
{
回声< TR>中;
回声< TD风格='宽度:10%;>中。 $行['ID']。 &所述; / TD>中;
回声< TD风格='宽度:10%;>中。 $行['NIMI']。 &所述; / TD>中;
回声< TD风格='宽度:10%;>中。 $行['Puhelin']。 &所述; / TD>中;
回声< TD风格='宽度:10%;'>< A HREF =邮寄地址。 $行[电子邮件。 '>中。 $行[电子邮件。 &所述; / a取代;&所述; / TD>中;
回声< TD风格='宽度:40%;>中。 $行['Viesti']。 &所述; / TD>中;
回声< TD风格='宽度:10%; >中。 $行['日']。 &所述; / TD>中;
回声< TD风格='宽度:10%;>中。 $行[IP。 &所述; / TD>中;
$ unwanteduser = $行['IP'];
回声<形式的行动='thissamepage'方法='后'><输入类型=隐藏值='$ unwanteduserNAME ='GTFO'><输入类型=提交名称='提交值='禁令'>中;
回声< / TR>中;
}
回声< /表>;
则mysql_close($ CON);
?>
< /身体GT;
< / HTML>
作为写评论,如果你把那些形成一个逻辑单元到它自己的功能部件,搞搞变得更加简单:
/ **
*添加一个IP禁止以.htaccess文件
*
* @参数字符串$ htaccess_file
* @参数字符串$ IP
*写了该文件,或FALSE的失败字节@返回INT数。
* /
功能htaccess_add_ban_ip($ htaccess_file,$ IP)
{
$ htaccess_original =的file_get_contents($ htaccess_file);
如果(假=== $ htaccess_original){
返回false;
}
$ htaccess_changed = str_replace函数(
'允许从所有',
从所有$ IP \ nallow拒绝,
$ htaccess_original,
$计数
);
如果($算!= 1){
返回false;
}
返回file_put_contents($ htaccess_file,$ htaccess_changed);
}
您那么只需要在您需要的功能调用该函数的地方:
$结果= htaccess_add_ban_ip($文件,127.0.0.1);
检查返回值来控制,如果事情进展的权利,例如:测试:
如果(假=== $结果){
死亡(sprintf的(无法写入.htaccess文件%S。,$文件));
}
如果($结果< 36){
死亡(sprintf的('写入.htaccess文件%s时很少字节(%D),这是没有意义的,请检查。',$结果,$文件));
}
死亡(sprintf的('成功写道IP%s到.htaccess文件%S(书面%d字节)。',$ IP,$文件,$结果));
在今后你再可以引入需要的功能(如文件锁定)功能里,你必须通常不会改变大多数脚本的其余部分。
如果你正在寻找一种方式,以简化连接和查询你的mysql数据库中的一点,看到还有这样的相关回答另一个问题:
- PHP文件不能进入code 某些部分
它包含一个MySQL类/对象与另一个示例演示如何使用/创建功能,使code更容易处理。
I'm trying to achieve a little app for my own use that reads the data sent to my database from contact form, and I want to be able to ban users that are not welcome by the contents of the contact form, and so on. So I, I have every users IP, that is sent with the form. But, it only saves deny from to database every time I click the ban button and I'm wondering why. Here's the whole code:
<?php
if(isset($_POST['submit'])) {
// 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('allow from all', "deny from "."$unwanteduser"."\nallow from all", $htaccess);
// And write the new string back to the file
file_put_contents('.htaccess', $new_htaccess);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Yhteydenottopyynnöt</title>
<style>
body{width:100%;}
tr:nth-child(even) { background: #ccc; }
</style>
</head>
<body>
<?php
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM wp_contactform");
$f = fopen(".htaccess", "a+");
$ip = $row['IP'];
fwrite($ip , $f);
fclose($f);
echo "<table border='1'>
<tr>
<th style='width:5%;'>ID</th>
<th style='width:10%;'>Nimi</th>
<th style='width:10%;'>Puhelin</th>
<th style='width:10%;'>Sposti</th>
<th style='width:40%;'>Viesti</th>
<th style='width:10%;'>Päivä</th>
<th style='10%;'>IP</th>
<th style='5%;'>Ban</th>
</tr>";
$i = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td style='width:10%;'>" . $row['ID'] . "</td>";
echo "<td style='width:10%;'>" . $row['Nimi'] . "</td>";
echo "<td style='width:10%;'>" . $row['Puhelin'] . "</td>";
echo "<td style='width:10%;'><a href='mailto:" . $row['Email'] . "'>" . $row['Email'] . "</a></td>";
echo "<td style='width:40%;'>" . $row['Viesti'] . "</td>";
echo "<td style='width:10%;' >" . $row['Day'] . "</td>";
echo "<td style='width:10%;'>" . $row['IP'] . "</td>";
$unwanteduser = $row['IP'];
echo "<form action='thissamepage' method='post'><input type='hidden' value='$unwanteduser' name='gtfo'><input type='submit' name='submit' value='Ban'>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</body>
</html>
As written in a comment, if you put those parts that form a logical unit into a function of it's own, things turn out to become more simple:
/**
* add an ip to ban to a .htaccess file
*
* @param string $htaccess_file
* @param string $ip
* @return int Number of bytes that were written to the file, or FALSE on failure.
*/
function htaccess_add_ban_ip($htaccess_file, $ip)
{
$htaccess_original = file_get_contents($htaccess_file);
if (false === $htaccess_original) {
return false;
}
$htaccess_changed = str_replace(
'allow from all',
"deny from $ip\nallow from all",
$htaccess_original,
$count
);
if ($count != 1) {
return false;
}
return file_put_contents($htaccess_file, $htaccess_changed);
}
You then only need to call that function at the place where you need the functionality:
$result = htaccess_add_ban_ip($file, '127.0.0.1');
Check the return value to control if things went right, e.g. for testing:
if (false === $result) {
die(sprintf('Could not write .htaccess file "%s".', $file));
}
if ($result < 36) {
die(sprintf('Very little bytes (%d) written to .htaccess file "%s", this makes no sense, please check.', $result, $file));
}
die(sprintf('Successfully wrote IP %s to .htaccess file "%s" (%d bytes written).', $ip, $file, $result));
In the future you then can introduce needed functionality (like file-locking) inside the function and you must normally not change most of the rest of your script.
If you are looking for a way to simplify connecting and querying your mysql database a little, see as well this related answer to a different question:
It contains a MySql class/object with another example how to use/create functions to make the code easier to deal with.
这篇关于问题创造一个充满活力的.htaccess禁令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!