Reputation: 55
I'm building a small php-based application which requires a "config.php" file containing a username and password. Rather than requiring the end user to modify "config.php" manually before they upload the application to their server, I would like to dynamically generate "config.php" from a setup form.
Basically, I'd like to use this:
<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 & Password">
</div>
</fieldset>
</form>
to create "config.php":
<?php
$username = 'entered username';
$password = 'entered password';
Upvotes: 1
Views: 198
Reputation: 14237
I would suggest file_put_contents()
:
$config[] = "<?php";
$config[] = "\$username = '$_POST['username']';";
$config[] = "\$password = '$_POST['password']';";
file_put_contents("config.php", implode("\n", $config));
Upvotes: 2
Reputation: 219814
A very basic example. This can be improved upon a lot.
<?php
$fp = fopen('config.php', 'w');
fwrite($fp, "<?php\n");
fwrite($fp, "\$username = '$_POST['username']';\n");
fwrite($fp, "\$password = '$_POST['password']';\n");
fclose($fp);
?>
Upvotes: 1