Change variable values in config file

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
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Change variable values in config file

Post by arukomp »

Hello,

Let's say I have a config.php file with following contents:

Code: Select all

<?php

$dbhost = "localhost";
$dbuser = "db_user";
$dbpass = "db_pass";
$dbname = "db_name";

?>
Now I need a script to be able to change only one variable's value without touching the others. For example, a user comes to Administration area, clicks on "MySQL settings" and then changes MySQL login details ($dbuser and $dbpass only). So how do I write the new values into config.php file without touching the others.

Thanks for any suggestions
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

Read the values from the config file and then write all values into the file.

Code: Select all

$new_db_host = 'localhost';
$new_db_user = 'db_user';
$new_db_pass = 'db_pass';
$new_db_name = 'db_name';

$content = '<'."?php\n\n".
    '$dbhost = "'.$new_db_host."\";\n".
    '$dbuser = "'.$new_db_user."\";\n".
    '$dbpass = "'.$new_db_pass."\";\n".
    '$dbname = "'.$new_db_name."\";\n\n?".'>';

$h = fopen('PATH/TO/CONFIG/FILE/config.php', 'w');
if ($h)
{
    write( $h, $content );
    fclose( $h );
}
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Thanks for that :) It works
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Be very, very careful about injecting dynamic php into files. There is a reason we use configuration files, such as xml, plaintext, ini, etc :wink:
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

So what do you want to say? I shouldn't use php config files? If so, then how do I store settings and access them other way?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

parse_ini_file would be my method. You can read/write the used config file as a normal file. Any PHP code inserted will not "run".
arukomp
Forum Contributor
Posts: 113
Joined: Sun Sep 24, 2006 4:22 am

Post by arukomp »

Thanks, but won't people be able to see ini file's contents if they open it through browser or download it (through browser)? Is it really secure?
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

You can place it outside of web-root or make it php-parsed and prepend with something like ;<? die(); ?>

However, in a trusted environment, php config file is still the best option, see http://de.php.net/var_export for the examples of proper usage).
Post Reply