2014-07-01 61 views
1

首先讓我開始說我知道這將是一個非常基本的問題。我是PHP的新手,並且爲此付出了一點努力。兩個Bootstrap模式可以編輯兩個不同的文件內容

簡介: 我想提出一個本地WAMP服務器上的小應用程序來管理一個基本的客戶數據庫,還可以管理所有發展我VHOST enntries。我知道編輯Windows主機文件時存在安全問題,因爲我將要求這些文件,但這只是本地網站。

我有以下的PHP

<?php include 'template-parts/header.php' /** calling of header(to make it uniform in all template file) **/?> 

<div class="container home"> 
    <h3> Delete </h3> 



    <div class="btn-group"> 
      <button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button> 
      <button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button> 
    </div> 


    <?php 

     // configuration 
     $url = 'delete.php'; 
     $file = 'C:/Windows/System32/Drivers/etc/hosts'; 

     // check if form has been submitted 
     if (isset($_POST['text'])) 
     { 
     // save the text contents 
     file_put_contents($file, $_POST['text']); 

     // redirect to form again 
     header(sprintf('Location: %s', $url)); 
     printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
     exit(); 
    } 

    // read the textfile 
    $text = file_get_contents($file); 
    ?> 


    <!-- Modal 1 --> 
    <div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
     <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title">Delete Host File Entry</h4> 
      </div><!-- /modal-header --> 
      <div class="modal-body"> 
       <div class="container-fluid"> 
        <div class="row"> 
         <div class="col-xs-12 col-md-12"> 
          <!-- HTML form --> 
          <form action="" method="post"> 
           <textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br /> 
           <p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p> 
         </div> 
        </div> 
       </div>       
      </div><!-- /modal-body --> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="submit" class="btn btn-primary">Save changes</button> 
      </div> 
      </form> 
     </div> <!-- /.modal-content --> 
    </div> <!-- /.modal-dialog --> 
</div> <!-- /.modal --> 


<!-- Modal 2 --> 
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title">Delete VHOST File Entry</h4> 
      </div><!-- /modal-header --> 
      <div class="modal-body"> 
       <div class="container-fluid"> 
        <div class="row"> 
         <div class="col-xs-12 col-md-12"> 
          <!-- HTML form --> 
          <form action="" method="post"> 
           <textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br /> 
         </div> 
        </div> 
       </div>       
      </div><!-- /modal-body --> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="submit" class="btn btn-primary">Save changes</button> 
      </div> 
      </form> 
     </div> <!-- /.modal-content --> 
    </div> <!-- /.modal-dialog --> 
</div> <!-- /.modal --> 
</div> 
</div> 
</body> 
</html> 

的第一模完美的作品,我按一下按鈕<button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>,這打開了引導模態,呈現文本區域內的Windows主機文件,並允許我添加和刪除隨意,我點擊模式的保存按鈕,它的作品。

問題我需要實現第二個模態窗口的相同動作,但是這需要編輯C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf'

我簡直不知道如何複製第一個正確工作的PHP,現在也可以使用VHOST編輯要求。

任何幫助或指針將不勝感激。

編輯

喜尼爾,我試圖把你的解決方案到位,在我的PHP有一些錯誤,我不是完全知道如何讓過去......

我現在有這文件:

<div class="container home"> 
<h3> Delete </h3> 



<div class="btn-group"> 
     <button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button> 
     <button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button> 
</div> 


<?php 
// check if form has been submitted 
if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it 

    // configuration 
    $url = 'delete.php'; 
    $file = 'C:/Windows/System32/Drivers/etc/hosts'; 

    // check if form has been submitted 
    if (isset($_POST['hostinput'])) 
    { 
    // save the text contents 
    file_put_contents($file, $_POST['hostinput']); 

    // redirect to form again 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
    exit(); 
    } 
    // read the textfile 
    $text = file_get_contents($file); 
} 
else if(!empty($_POST['vhostinput'])){ 
    // configuration 
    $url = 'delete.php'; 
    $file = 'C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf'; 

    // check if form has been submitted 
    if (isset($_POST['vhostinput'])) 
    { 
    // save the text contents 
    file_put_contents($file, $_POST['vhostinput']); 

    // redirect to form again 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
    exit(); 
    } 
    // read the textfile 
    $text = file_get_contents($file); 
} 


?> 


<!-- Modal 1 --> 
<div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
      <h4 class="modal-title">Delete Host File Entry</h4> 
     </div><!-- /modal-header --> 
     <div class="modal-body"> 
      <div class="container-fluid"> 
       <div class="row"> 
        <div class="col-xs-12 col-md-12"> 
         <!-- HTML form --> 
         <form action="" method="post"> 
          <textarea id="hostinput" name="hostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br /> 
          <p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p> 
        </div> 
       </div> 
      </div>       
     </div><!-- /modal-body --> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
      <button type="submit" class="btn btn-primary">Save changes</button> 
     </div> 
     </form> 
    </div> <!-- /.modal-content --> 
</div> <!-- /.modal-dialog --> 

<!-- Modal 2 --> 
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title">Delete VHOST File Entry</h4> 
      </div><!-- /modal-header --> 
      <div class="modal-body"> 
       <div class="container-fluid"> 
        <div class="row"> 
         <div class="col-xs-12 col-md-12"> 
          <!-- HTML form --> 
          <form action="" method="post"> 
           <textarea id="vhostinput" name="vhostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br /> 
         </div> 
        </div> 
       </div>       
      </div><!-- /modal-body --> 
      <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="submit" class="btn btn-primary">Save changes</button> 
      </div> 
      </form> 
     </div> <!-- /.modal-content --> 
    </div> <!-- /.modal-dialog --> 
</div> <!-- /.modal --> 
</div> 

而我得到的第二個呼叫的錯誤是: 注意:未定義的變量:文本在C:\用戶\ XXX \ XXX \ XXX \ devlogs \ delete.php線116上

回答

1
<button class="btn btn-primary btn-sm" data-target="#myModal1" data-toggle="modal" type="button"> Edit</button> <!-- first button for the first modal--> 

<button class="btn btn-primary btn-sm" data-target="#myModalA1" data-toggle="modal" type="button"> Comment</button> <!-- second button for the second modal--> 

<div id="myModal1" class="modal fade bs-example-modal-lg" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1"> 
<div id="myModalA1" class="modal" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1"> 

此代碼似乎相似到你的代碼,但我不太確定是否有一些差異(這一個我100%肯定它的工作完全正常)。

現在爲您的問題,你想提交的第二個模式的形式。有很多方法可以處理這個問題(比如使用ajax並保存所有內容,而不需要表單或刷新頁面),但是既然你說你是PHP的新手,並且你已經開始這樣做了......我可以建議的是那... ....

1)對於每個輸入你必須給它一個id和名字。所以你的投入應該是這樣的(你可以設置相同的名稱和ID對同一領域,但每場必須是唯一的):在你的PHP代碼

<textarea class="form-control" rows="15" id="hostinput" name="hostinput"></textarea> 
<textarea class="form-control" rows="15" id="vhostinput" name="vhostinput"></textarea> 

2)然後鍵入:

// check if form has been submitted 
    if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it 

    // save the text contents 
    file_put_contents($file, $_POST['text']); 

    // redirect to form again 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
    exit(); 
} 
esleif(!empty($_POST['vhostinput'])){ 
//SAVE THE VHOST 
} 

這樣你只要按下一個提交按鈕就可以嘗試檢查兩個值並將它們再次保存。

警告:此方法有點危險....可以說用戶編輯了第一個文本,但他點擊取消....然後編輯第二個文本,然後他點擊提交第二個文本...你實際上將節省他們的....

的方式,我通常這樣做是使用JavaScript和Ajax,所以我不必須刷新頁面也...

希望我幫助... 。

+0

嗨尼爾,我解決了一些問題......我在上面的編輯中粘貼了我的代碼。我似乎嚴重地超出了我的深度。我很抱歉,我正在研究這些錯誤,但似乎無法獲得任何地方。 – mobius2000

+0

刪除if(!empty .....)行並保持其他所有內容一致....讓我知道會發生什麼,如果有任何錯誤,請告訴我什麼不只寫行號... – Neal

+0

嗨尼爾,非常感謝你抽出時間......當我刪除if(!empty ...)行時,根本沒有任何錯誤,但是這兩個按鈕只打開PHP中出現的最後一個文件,在這種情況下是VHOSTS文件。幾乎就好像調用VHOSTS文件的後半部分取代了試圖調用HOST文件的前半部分。 – mobius2000

相關問題