944,191 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6271
  • PHP RSS
Jan 14th, 2007
0

Writing a config file via form.

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cashblogs is offline Offline
4 posts
since Jan 2007
Jan 14th, 2007
0

Re: Writing a config file via form.

Check out:
fopen()
fwrite()

Tough keep in mind that this is really insecure. To improve the security, you can move the config.php out of the web server root or place it in a password protected directory.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Jan 15th, 2007
0

Re: Writing a config file via form.

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: WYSIWYG-Editor
Next Thread in PHP Forum Timeline: option dropdown can't see options





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC