| | |
Writing a config file via form.
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2007
Posts: 4
Reputation:
Solved Threads: 0
Hello members
Is it possible to edit the values (say, admin logon/password) in a config file (say, config.php) WITHOUT editing the php script config.php??
In other words, can I write another file (configeditor.php) that will alter the values (or just SOME) based on data entered into in a form??
To do this would I have to use the set function?
Thanks
Is it possible to edit the values (say, admin logon/password) in a config file (say, config.php) WITHOUT editing the php script config.php??
In other words, can I write another file (configeditor.php) that will alter the values (or just SOME) based on data entered into in a form??
To do this would I have to use the set function?
Thanks
Its easier to do if you have every variable in the config file saved in an array.
Eg:
[php]
$myConfig = array();
$myConfig['db_user'] = 'username';
// etc...
[/php]
This allows you to iterate through each value in the $myConfig array and not through the values retrieved via HTTP...
eg:
[php]
include('config.php');
$config_str = "\$myConfig = array();\r\n"; // note you have to escape the $ character with \ so php treats it as a string literal instead of a variable designator..
foreach($myConfig as $key=>$val) {
$new_val = false;
if (isset($_REQUEST[$key])) {
// you know you have a value from HTTP to ovewrite the old one..
$new_val = $_REQUEST[$key];
} else {
// you should use the old value
$new_val = $myConfig[$key];
}
// you'll be creating a string representation of the PHP code to write to your config file here...
$config_str = "\$myConfig[{$key}] = '{$new_val}'; \r\n";
}
// write your new config string to the config file
if ($fp = fopen('/path/to/config.php', 'w')) {
fwrite($fp, $config_str, strlen($config_str));
} else {
echo 'Config file unwritable... '; // have to chmod
}
[/php]
usually, you'd want to check the original data type of the config value and cast this type to the value retrieved via HTTP, as data from HTTP always has a type of string for single values.. (I think)..
This way you can preserver boolean, int and floats.. etc.
Eg:
[php]
$myConfig = array();
$myConfig['db_user'] = 'username';
// etc...
[/php]
This allows you to iterate through each value in the $myConfig array and not through the values retrieved via HTTP...
eg:
[php]
include('config.php');
$config_str = "\$myConfig = array();\r\n"; // note you have to escape the $ character with \ so php treats it as a string literal instead of a variable designator..
foreach($myConfig as $key=>$val) {
$new_val = false;
if (isset($_REQUEST[$key])) {
// you know you have a value from HTTP to ovewrite the old one..
$new_val = $_REQUEST[$key];
} else {
// you should use the old value
$new_val = $myConfig[$key];
}
// you'll be creating a string representation of the PHP code to write to your config file here...
$config_str = "\$myConfig[{$key}] = '{$new_val}'; \r\n";
}
// write your new config string to the config file
if ($fp = fopen('/path/to/config.php', 'w')) {
fwrite($fp, $config_str, strlen($config_str));
} else {
echo 'Config file unwritable... '; // have to chmod
}
[/php]
usually, you'd want to check the original data type of the config value and cast this type to the value retrieved via HTTP, as data from HTTP always has a type of string for single values.. (I think)..
This way you can preserver boolean, int and floats.. etc.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- smb/// no host "null" (*nix Software)
- sourcing a python config file? (Python)
- how to set relative path in a .config file? (Java)
Other Threads in the PHP Forum
- Previous Thread: WYSIWYG-Editor
- Next Thread: Disable URL change via URL bar
| Thread Tools | Search this Thread |
apache api array beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external fcc file files folder form forms forum freelancing function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert ip javascript joomla limit link login mail malfunction menu method mlm mod_rewrite multiple mysql neutrality oop pageing pagerank paypal pdf php phpmysql play problem query question radio random recursion remote root script search select server sessions sms soap source space sql support! syntax system table template tutorial update upload url validator variable video web youtube






