如果isset$_POST输出失败


if isset $_POST output fail

我正试图为我制作的聊天创建一个禁止系统,它有一个单独的"控制台",只有管理员才能看到。我使用的是Mac OS 10.11.4,我是它的所有者,我使用的Mamp带有php版本5.6.0

Ban.php

    <?php
    $ban = $_POST['ban'];
    $mybfile = fopen("banned.txt", 'a');
    $txtb = ($ban." , ");
    //Makes sure ip banned it not an admins
    if (isset($_POST['ban'])) {
        //example ip addresses
        if ($ban === '1.1.1.1' || 192.168.1.132) {
            echo 'Can''t ban an Admin';
        } else {
            echo 'IP banned';
            fwrite($mybfile, $txtb);
            fclose($mybfile);
        }
    }
    ?>
    <style>
        .ban {
        background-color: black;
        width:30em;
        height:5em;
        color: #7ACC52;
        }
        .buttonBAN {
        border:1px solid black;
        width: 85px;
        height: 55px;
        background-color: white;
        color: black;
        position: absolute;
        }
    </style>
    <body>
    <form method="POST">
        BanCMD<br />
        <input type="text" name="ban" class="ban">
        <input type="submit" value="Enter" class="buttonBAN">
    </form>
    </body>

Chat.php:

   <?php
    require "blocked.php";
    require "connect.inc.php";
    require "core.inc.php";
    require "commands.php";
    $sent = $_POST['chat'];
    $myfile = fopen("chat.txt", 'a') or die("Unable to open file!");
    $txt = ($sent."'n");
    $first = getuserfield('username');
    $active = ($first.":".$ip_addr);
    $activef = fopen("ip-user.txt", 'a');
    $myFile = "domains/domain_list.txt";
    if (loggedin()) {
        echo 'Welcome, '.$first,'<br />';
            if ($first != 'SnR' || 'Koi') {
                fwrite($activef, $active."'n"."=");
            }
        } else if (!loggedin()) {
            die('Not logged in');
    }
    if (isset($_POST['chat'])) {
        if (!empty($sent)) {
            fwrite($myfile, $first.': '.$txt.'=');
            fclose($myfile);
        } else if (empty($sent)) {
            echo 'Cant send an empty message','<br />';
        }
    }
    $file = "chat.txt";
    $linecount = 0;
    $handle = fopen($file, "r");
    while(!feof($handle)){
      $line = fgets($handle);
      $linecount++;
    }
    fclose($handle);
    if ($linecount > 49) {
        unlink($file);
    } else {
        echo 'Line count: '.$linecount,'<br />';
    }
    echo 'Chat will reset at 50 lines','<br />';
    echo 'Your IP:';
    echo $ip_addr,'<br />';
    ?>
    <html>
        <head>
        </head>
        <body>
            <!-- <a href='active.txt'>Online users</a><br /> -->
            <iframe id='reload' src='refresh.php'>
               <fieldset class="field">
                        <div id="list"><p><?php
                            $filename = 'chat.txt';
                            $handle = fopen($filename, 'r');
                            $detain = fread($handle, filesize($filename));
                            $chat_array = explode('=', $detain);
                            foreach($chat_array as $chat) {
                              echo $chat.'<br />';
                            }
                       ?></p></div>
              </fieldset>
          </iframe>
          <form action="chat.php" method="POST">
              <input type="text" name="chat" class="textbox">
              <input type="submit" value="Send" class="button">
          </form>
        </body>
    </html>
    <?php
        if ($first == 'SnR' && 'Koi') {
            include 'AdminCMD.php';
            include 'ban.php';
    ?>
            <iframe id='reload' src='refresh2.php' class="field2">
                <fieldset class="field">
                </fieldset>
            </iframe>
    <?php
        }
    ?>

问题是,在你输入之前,一切都很好,无论你把什么放进盒子里,输出总是"不能禁止管理员",这意味着它不会写入给定的文件

谢谢你的帮助。

您的代码包含一个错误,导致无法写入文件。如果没有发送post数据,PHP代码的第一行$ban = $_POST['ban'];可能会失败。您需要首先检查是否设置了$_POST['ban']。您的代码的固定版本可以在下面找到。

<?php
    //Makes sure ip banned it not an admins
    if (isset($_POST['ban'])) {
        $ban = $_POST['ban'];
        $mybfile = fopen("banned.txt", 'a');
        $txtb = ($ban." , ");
        if ($ban === '1.1.1.1') {
            echo 'Can''t ban an Admin';
        } else {
            echo 'IP banned';
            fwrite($mybfile, $txtb);
            fclose($mybfile);
        }
    }
?>
<style>
    .ban {
        background-color: black;
        width:30em;
        height:5em;
        color: #7ACC52;
    }
    .buttonBAN {
        border:1px solid black;
        width: 85px;
        height: 55px;
        background-color: white;
        color: black;
        position: absolute;
    }
</style>
<body>
    <form method="POST">
        BanCMD<br />
        <input type="text" name="ban" class="ban">
        <input type="submit" value="Enter" class="buttonBAN">
    </form>
</body>

当我运行您的代码时,它返回未定义的索引ban,您可以尝试一下,并确保什么对您不起作用。

   <?php
    $ban = isset($_POST['ban']) ? $_POST['ban'] : null;
    $mybfile = fopen("banned.txt", 'a');
    $txtb = ($ban." , ");
    //Makes sure ip banned it not an admins
    if(isset($ban)){
    if ($ban === '1.1.1.1') {
                echo 'Can''t ban an Admin';
            } else {
                echo 'IP banned';
                fwrite($mybfile, $txtb);
                fclose($mybfile);
            }
    }
    ?>
   <style>
    .ban {
    background-color: black;
    width:30em;
    height:5em;
    color: #7ACC52;
    }
    .buttonBAN {
    border:1px solid black;
    width: 85px;
    height: 55px;
    background-color: white;
    color: black;
    position: absolute;
    }
</style>
<body>
<form method="POST">
    BanCMD<br />
    <input type="text" name="ban" class="ban">
    <input type="submit" value="Enter" class="buttonBAN">
</form>
</body>