First, lets separate your constants to make it easier to edit.
We’ll call it constants.php
[php]<?php
//Constants
//Using a format of /* VARIABLE */ define(‘YOUR_CONSTANT’, “YOUR_VALUE”);
/* USER_NAME / define(“USER_NAME”,"");
/ PASS_WORD / define(“PASS_WORD”,"");
/ DATABASE / define(“DATABASE”,"");
/ SERVER */ define(“SERVER”,"****");
?>[/php]
You’ll notice the special format noted in the file. This will be used by the parser we’ll make to edit the file.
This is our class we’ll use to update the constants
[php]<?php
class Const_Editor
{
function OpenFile ($file_name)
{
//Save our file path & name for saving it later/
$this->file_name = $file_name;
//This will read all of our code lines into an array.
$this->codeLines = file($file_name);
}
function Update_Constant ($constant_key, $constant_name, $constant_value)
{
//Loop through the array
for($i = 0; $i < count($this->codeLines); $i++)
{
//find the line that we want to change
if(strstr($this->codeLines[$i], “/* " . $constant_key . " /"))
{
//update the array element to contain our new constant.
$this->codeLines[$i] = "/ " . $constant_key . " */ define(” . chr(34) . “’” . $constant_name . “’, '”. $constant_value . “’);”;
}
}
}
function Save_File ()
{
$saveFile = fopen($this->file_name, “w”);
$saveData = implode(chr(10), $this->codeLines);
fwrite($saveFile, $saveData, strlen($saveData));
fclose($saveFile);
}
}
?>[/php]
Finally lets put it all together.
[php]<?php
if($_GET[‘action’] == “update”)
{
include(“constants_editor.php”);
$constEdit = new Const_Editor();
$constEdit->OpenFile(“constants.php”);
foreach($_POST as $k => $v)
{
$constEdit->Update_Constant($k, $k, $v);
}
$constEdit->Save_File();
}
include(“constants.php”);
?>
Server
User
pass
DB
[/php]
I just tested all this code aswell and is working.