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
arukomp
Forum Contributor
Posts: 113 Joined: Sun Sep 24, 2006 4:22 am
Post
by arukomp » Sun May 06, 2007 1:22 am
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
kaszu
Forum Regular
Posts: 749 Joined: Wed Jul 19, 2006 7:29 am
Post
by kaszu » Sun May 06, 2007 5:43 am
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 » Sun May 06, 2007 1:12 pm
Thanks for that
It works
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun May 06, 2007 5:06 pm
Be very, very careful about injecting dynamic php into files. There is a reason we use configuration files, such as xml, plaintext, ini, etc
arukomp
Forum Contributor
Posts: 113 Joined: Sun Sep 24, 2006 4:22 am
Post
by arukomp » Mon May 07, 2007 3:11 am
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?
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon May 07, 2007 3:41 am
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 » Mon May 07, 2007 4:29 am
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?
stereofrog
Forum Contributor
Posts: 386 Joined: Mon Dec 04, 2006 6:10 am
Post
by stereofrog » Mon May 07, 2007 5:34 am
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).