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>