如果isset $ _POST输出失败

时间:2016-04-30 01:00:26

标签: php isset

我正在尝试为我创建的聊天创建一个禁止系统,它有一个单独的“控制台”,只有管理员可以看到。我使用Mac OS 10.11.4,我是所有者,我正在使用Mamp与php版本5.6.10

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
        }

    ?>

问题是一切都很好,直到你输入一个输入,无论你放入框中输出总是“不能禁止管理员”意味着它不写入给定文件

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的代码包含错误,导致无法写入文件。如果没有发送帖子数据,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>

答案 1 :(得分:0)

当我运行您的代码时,它会返回未定义的索引 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>