1

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 &amp; Password">
    </div>
</fieldset>
</form>

to create "config.php":

<?php

$username = 'entered username';
$password = 'entered password';
3
  • Use frwite() Commented Aug 3, 2012 at 17:19
  • 1
    What? No, no, no, no, no. Usernames and passwords belong in a database. Period. And the passwords need to be secured through a hash. Commented Aug 3, 2012 at 17:20
  • 1
    @Matt - I agree in principle, but there's nothing wrong with storing user login data for small applications in the file system so long as it's (a) Outside the document root; and (b) You hash the password data (as you mentioned) Commented Aug 3, 2012 at 18:21

2 Answers 2

2

I would suggest file_put_contents():

$config[] = "<?php";
$config[] = "\$username = '$_POST['username']';";
$config[] = "\$password = '$_POST['password']';";

file_put_contents("config.php", implode("\n", $config));
Sign up to request clarification or add additional context in comments.

3 Comments

You're not using the correct operators. You need to use the concatination operator: .=... otherwise you'll just have $config = "\$password = '$_POST['password']';\n" in the config file.
True, updated to use my preferred syntax. I prefer arrays and implode for scalability, although something of this small of scale, concating the vars would be fine.
I like the file_put_contents() method as well
1

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.