首先讓我開始說我知道這將是一個非常基本的問題。我是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">×</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">×</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">×</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">×</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上
嗨尼爾,我解決了一些問題......我在上面的編輯中粘貼了我的代碼。我似乎嚴重地超出了我的深度。我很抱歉,我正在研究這些錯誤,但似乎無法獲得任何地方。 – mobius2000
刪除if(!empty .....)行並保持其他所有內容一致....讓我知道會發生什麼,如果有任何錯誤,請告訴我什麼不只寫行號... – Neal
嗨尼爾,非常感謝你抽出時間......當我刪除if(!empty ...)行時,根本沒有任何錯誤,但是這兩個按鈕只打開PHP中出現的最後一個文件,在這種情況下是VHOSTS文件。幾乎就好像調用VHOSTS文件的後半部分取代了試圖調用HOST文件的前半部分。 – mobius2000