PHP HelpPHP Help

Online Community that helps beginners learn PHP,
and webmasters solve PHP coding problems

since 1999

Basics of the Configuration File

Writing the Configuration File

Here is a function that will generate and write our configuration file:

function write_config()


// For every setting, add a global variable

global $db_user,$db_pass,$db_host,$db_name,$db_table;


// Prepare settings

// Using single quotes is easier because you don't have to escape them. For every setting, add a line.

$settings "<?php\n";

$settings .= "// Do not edit this file. Generated by admin script.\n";

$settings .= "// Database settings\n";

$settings .= "\$db_user = '$db_user';\n";

$settings .= "\$db_pass = '$db_pass';\n";

$settings .= "\$db_host = '$db_host';\n";

$settings .= "\$db_name = '$db_name';\n";

$settings .= "\$db_table = '$db_table';\n";

$settings .= "// Paths\n";

$settings .= "\$base_path = '$base_path';\n";

$settings .= "?>\n";

// Write out new initialization file

$fd fopen'/somepath/config.php'"w" )

or die (
"Cannot create configuration file.");

fwrite$fd$settings );

fclose$fd );


Note:  Because I made this a function, we need to include any variables we are going to write into the configuration as global variables or function parameters. I chose to make all variables used by the configuration file globals. They will likely be global to the whole application anyway (unless you're using classes).

Pages:  1   2   3   4Next:  Getting Input from the User »

Copyright © 2017