ivytony Posted June 18, 2008 Share Posted June 18, 2008 I would like my website to be configured in a .php file for the website title, meta keywords, description, contact email, etc. I wonder how to write to such a .php file that has config. information in such rows as below: $website_title = 'PHP freaks'; $website_meta_key = 'PHP, solution, freaks'; .... $website_email = 'phpfreaks@gmail'; And the second question is how to read the file? Thanks! Link to comment https://forums.phpfreaks.com/topic/110820-how-to-write-to-a-file-as-configuration/ Share on other sites More sharing options...
lemmin Posted June 18, 2008 Share Posted June 18, 2008 One way to do it would be to make the configuration file without the dollar signs: file.cfg website_title=PHP freaks website_meta_key=PHP, solution, freaks .... website_email=phpfreaks@gmail Then, in the code: $conVars = array(); foreach (explode("\n", file_get_contents("file.cfg")) as $value) { $temp = explode("=", $value); $conVars = array_merge($conVars, array($temp[0] => $temp[1])); } Then you should have an array of the variables with their values. You can even use the list method if you want to have them as actual variables instead of array keys. Link to comment https://forums.phpfreaks.com/topic/110820-how-to-write-to-a-file-as-configuration/#findComment-568603 Share on other sites More sharing options...
xtopolis Posted June 18, 2008 Share Posted June 18, 2008 You could do what you're doing and save it as whatever you want. config.php <?php $website_title = 'PHP freaks'; $website_meta_key = 'PHP, solution, freaks'; .... $website_email = 'phpfreaks@gmail'; ?> To read this: index.php <?php include ('config.php'); echo '<html>'; echo '<head><title>' . $website_title . '</title>'; //sets page title to PHP Freaks ?> Personally I would prefer to use globals instead of regular vars. config_globals.php <?php DEFINE("W_TITLE","PHP Freaks"); DEFINE("W_META_KW","PHP, solution, freaks"); .... DEFINE("W_EMAIL","phpfreaks@gmail"); ?> index.php <?php include('config_globals.php'); echo '<html>'; echo '<head><title>' . W_TITLE . '</title>'; //sets page title to PHP Freaks //etc.. notice that for global vars you do not use a $ prefix, and you write them like above ?> Link to comment https://forums.phpfreaks.com/topic/110820-how-to-write-to-a-file-as-configuration/#findComment-568607 Share on other sites More sharing options...
sasa Posted June 18, 2008 Share Posted June 18, 2008 try <?php $fileName = 'test.dat'; // write file $a = fopen($fileName, 'w'); fwrite($a, 'website_title = PHP freaks'."\n"); fwrite($a, 'website_meta_key = PHP, solution, freaks'."\n"); fwrite($a, '[sasa]'."\n"); fwrite($a, 'blah = something'."\n"); fclose($a); //read file $a = parse_ini_file($fileName,1); print_r($a); //or echo "<hr />\n"; $a = parse_ini_file($fileName,0); print_r($a); ?> Link to comment https://forums.phpfreaks.com/topic/110820-how-to-write-to-a-file-as-configuration/#findComment-568613 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.