Erm, you want to rewrite .php files to replace variable contents?
I would recommend using INI files. I made a post that (to my knowledge) accurately reads & modifies INI files, and you can view the original post here. I'll show you how to modify it to suit your needs, though.
Everything from this line:
$config['serveraddress'] = 'www.bradgrafelman.com'; // Example of changing/adding a setting
and on in that post is the modifying portion of the script; remove it or move it into a separate function to separate the read/write parts of the script. Here is what it would look like:
function read_ini_file($path, &$config) {
if(!is_file($path)) die('Error: File does not exist (' . $path . ')');
if(filesize($path) === 0) return 0;
$file = file($path) or die('Error: Could not open file for reading (' . $path . ')');
$config = array();
$file = implode('', $file);
$file = explode("\n", $file);
foreach($file as $line) {
if(strstr($line, ' =')) {
$split = explode(' =', $line);
$config[$split[0]] = trim($split[1]);
} // End if
} // End foreach
} // End function
function write_ini_file($path, &$config) {
if(false === ($fp = fopen($path, 'w'))) die('Error: Could not open file for reading (' . $path . ')');
foreach($config as $setting => $value) {
if($value != NULL) fwrite($fp, "$setting = $value\n"); else fwrite($fp, "$setting =\n");
} // End foreach
fclose($fp);
} // End function
To read/change the values using a form, you would do something like this:
$path = '/path/to/config.ini';
if(isset($_POST['submit'])) {
read_ini_file($path, $variables);
$variables['field1'] = $_POST['field1'];
write_ini_file($path, $variables);
echo 'Variables successfully updated!';
} else {
read_ini_file($path, $variables);
// Output HTML form, using a foreach() loop to get all varialbes, like so:
foreach($variables as $name => $value) {
echo $name . ': <input type="text" name="' . $name . '" value="' . $value . "\">\n";
}
}
Confused yet? :p
NOTE: This .ini function is NOT compatible with multi-line variable contents. If you want to use multiple lines, encode the line endings in the write function as special phrases, such as '~!br!~' so you can [man]str_replace/man the phrase to "\n" in the read function. Let me know if you need help with this.
Also note that this is merely a suggestion. If you still wanted to use PHP files, you could probably use [man]preg_replace/man to match the text in the PHP file and update it's value. Seemed messy to me and very hard to keep secure, so I suggested my INI method of setting up variables.