Configuration Editor Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Configuration Editor Page

Post by socket1 »

I need to know what is the best way to make a page that edits a configuration file, ex)

File: Config.php

Code: Select all

 
<?php
$MySQL['host'] = "localhost";
$MySQL['user'] = "root";
$MySQL['pass'] = "test";
?>
 
File: config_edit.php

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 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?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Configuration Editor Page

Post by Jade »

Well, you could use a database and have it dynamically pull the values from the table. That way you wouldn't have to re-write the config file each time, just query the database for the appropriate values.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Configuration Editor Page

Post by socket1 »

Well, I was going to do that eventually for some values, but the MySQL connection details, should they just be static in one little config file ?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Re: Configuration Editor Page

Post by toasty2 »

One method I like to use is an INI file, but named with a .php extension. So that people can't see the settings though, you put some php exit() or die() code on the first line, commented so the ini parser doesn't mind.

config.php
[ini];<?php exit;?>host="localhost"user="root"pass="test" [/ini]

Instead of including this page, you access it with:

Code: Select all

 
$config = parse_ini_file('config.php');
// You can access the individual settings like so:
$connection = mysql_connect($config['host'], $config['user'], $config['pass']);
 
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Configuration Editor Page

Post by socket1 »

Interesting concept, thanks.

For other variables I did something like this

To set the variables

config.php

Code: Select all

 
$MySQL['host'] = "localhost";
$MySQL['user'] = "root";
$MySQL['pass'] = "pass";
$MySQL['database'] = "db";
 
$result = mysql_query("SELECT * FROM variables) or die (mysql_error());  
while($row = mysql_fetch_array( $result )) {
$Site[''.$row['variable'].''] = $row['data'];
}
 

edit.php

Code: Select all

 
$result = mysql_query("SELECT * FROM variables));  
while($row = mysql_fetch_array( $result )) {
echo = '<strong>'.ucwords($row['variable']).'</strong> --<input type="text" name="'.$row['variable'].'">';
}
 
With the appropriate form and submit tags around that code. submitting the form would post the info to edit_process.php



edit_process.php

Code: Select all

 
foreach($_POST as $variable => $value) {
mysql_query("UPDATE variables SET value='".$value."' WHERE variable='".$variable."'");
}
 
Post Reply