0

お問い合わせフォームからデータベースに送信されたデータを読み取る自分用の小さなアプリを実現しようとしています。お問い合わせフォームの内容によって歓迎されないユーザーを禁止できるようにしたいなどです。だから私は、フォームで送信されたすべてのユーザーの IP を持っています。しかし、禁止ボタンをクリックするたびに拒否をデータベースに保存するだけで、なぜだろうと思っています。コード全体は次のとおりです。

<?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&auml;iv&auml;</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>
4

2 に答える 2

1

$unwanteduserには、送信後にフォームからの値が割り当てられません

これを試して

<?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>
$unwanteduser = $_POST['gtfo']; 
$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);
}
?>

情報: フォームを送信すると、ページの更新のように見えるため、フォームを送信する前に $unwanteduser で割り当てたものはすべて失われます

私は混乱しています

$result = mysql_query("SELECT * FROM wp_contactform");
$f = fopen(".htaccess", "a+");
$ip = $row['IP'];

この時点で、 の値は$row['IP']? 何を追加しようとしていますか??

于 2012-07-20T14:41:45.290 に答える
0

コメントに書かれているように、論理ユニットを形成する部分を独自の関数に入れると、物事はより単純になります。

/**
 * 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);
}

次に、機能が必要な場所でその関数を呼び出すだけです。

$result = htaccess_add_ban_ip($file, '127.0.0.1');

戻り値をチェックして、テストなどでうまくいったかどうかを制御します。

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));

将来的には、必要な機能 (ファイルのロックなど) を関数内に導入することができますが、通常はスクリプトの残りのほとんどを変更してはなりません。

mysql データベースの接続とクエリを少し簡素化する方法を探している場合は、別の質問に対するこの関連する回答も参照してください。

コードを扱いやすくするために関数を使用/作成する方法の別の例とともに、MySql クラス/オブジェクトが含まれています。

于 2012-07-21T09:35:23.340 に答える