PHP Config Creator

By F*U*R*B*Y* on Jun 09, 2010

For those that want to create a page that will create a base config.php file for them.

<?php

    if (isset($_POST['dbserver'])) {
        $data = '<?php
            $host=$_POST['dbserver'];
            $username=$_POST['dbuser'];
            $password=$_POST['dbpass'];
            $db_name=$_POST['dbname'];
            mysql_connect($host, $username, $password)or die("cannot connect");
            mysql_select_db($db_name)or die("cannot select DB");
            ?>';
        $file = "config.php";

        $handle = fopen($file, 'a');
        if (fwrite($handle, $data) === FALSE) { echo "Can not write to (".$file.")"; }
        echo "Succesfully Wrote to (".$file.")";
        fclose($handle);
    }
    else {
        echo "<form name=\"Config Creation\" method=\"post\" action=\"".$PHP_SELF."\">";
        echo "Database Host: <input type=\"text\" name=\"dbserver\" value=\"localhost\"><br>";
        echo "Database User: <input type=\"text\" name=\"dbuser\" value=\"root\"><br>";
        echo "Database Pass: <input type=\"password\" name=\"dbpass\" value=\"password\"><br>";
        echo "Database Name: <input type=\"text\" name=\"dbname\" value=\"forums\"><br>";
        echo "<input type=\"submit\" value=\"Create Config\"><input type=\"reset\" value=\"Reset\">";
        echo "</form>";
    }
?>

Comments

Sign in to comment.
PATX   -  Jun 09, 2010

Cool. I'll give it a 5. A nice average script. Personally I don't really like PHP at all, but its still cool. Good job.

 Respond  
sean   -  Jun 09, 2010

not bad, relatively useful. a couple of recommendations though.

1) why are you putting your variables in quotes? you don't need to.
$host="'.$_POST['dbserver'].'"; should be: $host = $_POST['dbserver'];

2) check out file_put_contents, it'll save you some trouble - http://php.net/manual/en/function.file-put-contents.php

3) since this is primarily targeted towards inexperienced individuals, i'd add some error checking before just throwing and accepting random post data

4) if you use single quotes in your echo statements ( echo statements, that you could easily avoid using at all ), you wont have to escape your html double quotes

rated 4/10 for mediocre. slightly better coding practices and error checking should be implemented ( again, we really need a better rating system )

CrandellWS  -  Sep 21, 2015

I reworked the script and created a gist at https://gist.github.com/CrandellWS/40d3618576bec5d19226

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.