Getting user settings from files

hi i was wondering how i would

  1. Write user defined settings to a file - More than 1 per file

  2. Read each seperate setting and store as variables in php

example

set db setting

file settings.txt

localhost
username
password
database_name

and then how to set

$server_name = localhost from settings.txt

Here is the sample code to give you an idea:
settings.php

[code]<?php
// reading settings from the text file

$setting1 = ‘’;
$setting2 = ‘’;

if(is_file(‘settings.txt’)){
$lines = file(‘settings.txt’);
$setting1 = $lines[0];
$setting2 = $lines[1];
}
?>

Setting 1:
Setting 2:
[/code]

save.php
[php]<?php
// save settings to a text file

$fp = fopen(‘settings.txt’,‘w’);
if($fp){
fputs($fp,$_POST[“setting1”]."\n");
fputs($fp,$_POST[“setting2”]."\n");
}
fclose($fp);
?>[/php]

You may want to add validation of user input (i.e. strip special chars, new lines etc.)
And if you are using PHP 5, you can use file_put_contents() function to write data to a file.

Sponsor our Newsletter | Privacy Policy | Terms of Service