如何生成“;config.php”;通过PHP表单


How to Generate "config.php" via PHP Form?

我正在构建一个基于php的小型应用程序,它需要一个包含用户名和密码的"config.php"文件。与其要求最终用户在将应用程序上传到服务器之前手动修改"config.php",我更愿意从设置表单中动态生成"configphp"。

基本上,我想使用这个:

<form method="POST" action="?setup-config">
<fieldset>
    <div class="clearfix">
        <label for="username">Desired User Name</label>
        <div class="input">
            <input type="text" name="username" id="username">
        </div>
    </div>
    <div class="clearfix">
        <label for="password">Desired Password</label>
        <div class="input">
            <input type="password" name="password" id="password">
        </div>
    </div>
    <div class="actions">
        <input type="submit" value="Save Username &amp; Password">
    </div>
</fieldset>
</form>

创建"config.php":

<?php
$username = 'entered username';
$password = 'entered password';

我建议file_put_contents():

$config[] = "<?php";
$config[] = "'$username = '$_POST['username']';";
$config[] = "'$password = '$_POST['password']';";
file_put_contents("config.php", implode("'n", $config));

非常的基本示例。这可以在上得到很大改进

<?php
$fp = fopen('config.php', 'w');
fwrite($fp, "<?php'n");
fwrite($fp, "'$username = '$_POST['username']';'n");
fwrite($fp, "'$password = '$_POST['password']';'n");
fclose($fp);
?>