3

Ok so I want to do something similar to how Simple Machine Forum edits some variable within the Admin panel on the forum. For instance I have a settings.php file. I want tool.php to open and find all declared php variables in the settings.php like $background_color='Orange' and output in tools.php Background Color: Orange inside of a form tag that, once changed to 'Purple' and submitted, replaces 'Orange' with 'Purple' and now if i open 'settings.php' the variable inside the file is now "$background_color='Purple'" without having to go in an manually edit it. I can just submit the new data for the variable and replace the old.

Bart
  • 107
  • 4
  • [This post](https://stackoverflow.com/questions/11901521/replace-string-in-text-file-using-php) may help you. – cachique Jul 06 '17 at 05:23
  • 1
    This is a notoriously precarious situation. First off, from a security standpoint there are many types of permissions situations that will virtually guarantee this won't work. It's also potentially a huge security hole because when you have a system that can write .php files, any mistake you make or that the user makes, is a major vector into exploitation and privileged code execution. Overall it's a bad idea. Most packages have a simple limited config.php file which just requires database credentials, and then keeps the majority of your name=value variables in the database itself. – gview Jul 06 '17 at 05:49

3 Answers3

1

To keep the data persistent, you will need to store data in a database or separate file e.g. JSON file. Then you can use that data everywhere it is needed.

Here is an example using a JSON file.

setting_variables.json:

{
    "background": "red",
    "color": "white"
}

settings.php

$settings = json_decode(file_get_contents("settings_variables.json"), true);
$background = $settings['background'];

tools.php

include ("settings.php");

// If form is posted
if(isset($_POST['submit'])){
    /* Updating variables in JSON file*/

    //Get settings_json file
    $setting_vars = json_decode(file_get_contents('settings_variables.json'), true);

    // Update variable
    $background = $setting_vars['background'] = $_POST['color'];

    // Store variable value to JSON file
    file_put_contents("settings_variables.json", json_encode($setting_vars));
}

?>

<form method="post" <?php echo "style='background-color:".$background . "'"; ?>>
    <select name="color">
    <option value="red">red</option>
    <option value="blue">blue</option>
    <option value="orange">orange</option>
    <option value="yellow">yellow</option>
    <option value="purple">purple</option>
  </select>
  <input type="submit" name="submit"/>
</form>
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
  • Thanks :D I wanted to avoid using the database option, I had already considered it. – Bart Jul 07 '17 at 12:20
0

You can do this using database fetch that value from database in settings.php and update that from your tools.php or wherever you want .

rahul singh
  • 451
  • 1
  • 3
  • 17
0

To load the variables in tool.php, simply include("settings.php").

In order to change settings, you will need to modify settings.php.

The easiest way to do this should be by regex-replacing the old value with the new one, e.g. ["\$background_color *= *'\d*'"] and replace that with ["\$background_color = 'Purple'"].

Just overwrite the file with the new string.

I'm not sure I escaped everything correctly, but this way you should be able to replace the whole file.

See http://php.net/manual/en/function.preg-replace.php

The drawback is, that if the settings file is very large, it may take some time to do that, although that will probably not be a problem for your use case.

alternatively, you could read the file line by line and replace that (either manually or by using regex-replacing again.

Josef Hoppe
  • 376
  • 2
  • 9