In my Admin area I display all the configuration values for my login script (eg. Site URL, Site Name etc...) After the Admin changes any of the values how do I then save the changes back to my configuration file overwriting the values that were already there?
I am creating a class at the moment which writes and saves configuration files similar to the httpd.conf file. I'll post it here when I'm done. You may also want to look at the PHP parse_ini_file() function. There are some good examples in the user comments of functions which also write ini files too.
create a file with the settings laied out in this way
setting=value
;comment
then in php have a function to read the list
PHP Code:
<?php
$cfgfile = "scripts/config.txt";
//open the config file to read
$cfg = fopen($cfgfile,"r");
if ($cfg)
{
$data = fread($cfg,filesize($cfgfile));
//split by new lines
$lines = split("\n",$data);
//$lines is an array of settings and values
for ($d=0;$d<sizeof($lines);$d++)
{
if (substr($lines[$d],0,1) != ";")
{
$bits = explode("=",$lines[$d]);
$CFG[$bits[0]] = $bits[1];
// if its not a comment line
}
}
}
else
{
die("Cannot Read Config File ( $cfgfile )");
}
fclose($cfg)
// to use the settings its CFG[SETTING] the value will be stored in this 2D array
?>
try that :P
example would be
CFG['setting'] would equal "value"
EDIT : Removed extra )
Last edited by señorbadger; Sep 16th, 2004 at 03:47 AM.
$CFG = array of $CFG[Setting] settings names
functions in class
int add_setting($setting, $value, $file)
$setting = setting name
$value = setting value
$file = file to save to
This function will update if it already exsists
int delete_setting($setting, $file)
$setting = setting name
$file = file to delete from
int update_setting($setting, $newvalue, $file)
$setting = setting name
$newvalue = the new setting value
$file = file to save to
This function will create setting if not found
int is_setting($setting, $file)
array read_config($file)
$file = file to read from
will return array structure of object[setting] = value
Last edited by señorbadger; Sep 16th, 2004 at 05:32 AM.