Configuration Editor Page
Posted: Wed Dec 10, 2008 3:37 pm
I need to know what is the best way to make a page that edits a configuration file, ex)
File: Config.php
File: config_edit.php
I need to make a page that gets all these variables and makes them into a form, so one can edit the variables and remake the config.php file with the new variables.
I was doing this using a static form posting to a page that use fwrite to write the entire php file over again.
Is there any better way to do this?
File: Config.php
Code: Select all
<?php
$MySQL['host'] = "localhost";
$MySQL['user'] = "root";
$MySQL['pass'] = "test";
?>
Code: Select all
<?php
if (isset($_POST['user'], $_POST['pass'], $_POST['host'])) {
$configFile = "config.php";
$handle = fopen($configFile, 'w') or die("Cant open config file");
$input = '<?php
$mysql[\'user\'] = "'.$_POST['user'].'";
$mysql[\'pass\'] = "'.$_POST['pass'].'";
$mysql[\'host\'] = "'.$_POST['host'].'";
?>
';
fwrite($handle, $input);
fclose($handle);
}
else
{
require_once('config.php');
?>
<form action="config.php" method="post">
mysql_user : <input type="text" name="user" value="<?php echo $MySQL['user']; ?>" /><br />
mysql_pass : <input type="text" name="pass" value="<?php echo $MySQL['pass']; ?>" /><br />
mysql_host : <input type="text" name="host" value="<?php echo $MySQL['host']; ?>" /><br />
<input type="submit" value="config">
</form>
<?php
}
?>
I was doing this using a static form posting to a page that use fwrite to write the entire php file over again.
Is there any better way to do this?