aximbigfan Posted February 3, 2008 Share Posted February 3, 2008 Hi all, this is my first post here. I have recently started to learn PHP. The biggest app I have made is a log viewer/manipulator, about 500 lines. Anyway, Anyone know how to write configuration files in PHP? I have though of a few ways, but non work really well. Basically, I just want to write variables and values, to be used by a PHP app for its configuration. For example have a webui where the user says that they want "white" to be a background color, and "blue" be the text color. So they click on submit, and the PHP script opens config.php, and writes the following to it... $bgcolor = "white"; $textcolor = "blue"; Chris Quote Link to comment https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/ Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 User configurable options are easier to store in a database, and its simple to achieve. settings (table) settings ( id INT AUTO_INCREMENT, key TEXT, val TEXT ); Then to insert bgcolor and textcolor. INSERT INTO settings (key,val) VALUES ('bgcolor','white'); INSERT INTO settings (key,val) VALUES ('textcolor','blue'); To retrieve the bgcolor... SELECT val FROM settings WHERE key = 'bgcolor'; Quote Link to comment https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/#findComment-456493 Share on other sites More sharing options...
aximbigfan Posted February 3, 2008 Author Share Posted February 3, 2008 Thanks, but I really need the values to be stored as variables in a .php file. Chris Quote Link to comment https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/#findComment-456508 Share on other sites More sharing options...
trq Posted February 3, 2008 Share Posted February 3, 2008 Take a look at fwrite then. Simple example. <?php $str = "\$bgcolor = \"white\";\n\$textcolor = \"blue\";"; if ($f = fopen('config.php','a')) { if (fwrite($f,$str)) { echo "config saved"; } else { echo "failed to write config"; } fclose($f); } else { echo "failed to open file"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/#findComment-456536 Share on other sites More sharing options...
aximbigfan Posted February 3, 2008 Author Share Posted February 3, 2008 Take a look at fwrite then. Simple example. <?php $str = "\$bgcolor = \"white\";\n\$textcolor = \"blue\";"; if ($f = fopen('config.php','a')) { if (fwrite($f,$str)) { echo "config saved"; } else { echo "failed to write config"; } fclose($f); } else { echo "failed to open file"; } ?> So, it's that easy? I have used fwrite() before, but I wasn't sure how I would escape variables. Thanks a whole lot!!! Topic solved. Chris Quote Link to comment https://forums.phpfreaks.com/topic/89139-solved-writting-configuration-files/#findComment-456539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.