2016-04-30 70 views
0

我正在嘗試爲我創建的聊天創建一個禁止系統,並且它有一個只有管理員才能看到的單獨的「控制檯」。即時通訊使用的是Mac OS 10.11.4,我的主人和我使用甲基苯丙胺與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 
     } 

    ?> 

的問題是一切都很好,直到你給一個輸入,不管你把什麼東西放到框中輸出始終是「不能禁止管理員」,這意味着它不寫入給定的文件

謝謝你的任何幫助。

+0

'($ ban ==='1.1.1.1')'如果是這樣就更改。它有什麼區別? –

+0

不,它沒有區別 –

+0

'var_dump($ _POST)'在一開始。你有什麼'禁令'? –

回答

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> 
+0

仍然有同樣的問題 –

+0

@janeboe我在我的機器上本地運行代碼沒有問題,一定是你的PHP安裝有問題。你有寫入權限的文件將被保存的位置? – Flibio

+0

是的,我是計算機的管理員/所有者(Mac OS 10.11.4),並且使用PHP版本5.6.10 –

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> 
+0

,修復了未定義的索引但仍然存在相同的問題 –