1

I'm extending an application that stores configuration data in a php file. The file looks like this:

defined('DB_NAME') or define('DB_NAME', '');
defined('DB_USERNAME') or define('DB_USERNAME', '');
defined('DB_PASSWORD') or define('DB_PASSWORD', '');

Say, I want to make the parameter DB_NAME configurable from within the php application itself, thus avoiding manual changes in config.php. I thought of reading the file, searching for the string 'DB_NAME', do some string manipulation, and write the content back. However, this does not seem very elegant. Is there any other way?

2
  • Use a configuration file class implementation. Commented Jan 14, 2017 at 12:46
  • I'm no expert, but what stops you from receiving the variable from a POST and then setting it with define() ? Commented Jan 14, 2017 at 13:30

2 Answers 2

1

I suggest you avoid PHP source code and switch to any standard data format where you already have encoders and decoders. There's a lot to choose from:

Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely how I will do it in my own projects in the future!
0

One way to do it:

  • Copy the entire config in a config.tpl, to be used as a template
  • alter it to contain unique placeholders like this: defined('DB_NAME') or define('DB_NAME', '%DB_NAME%');
  • write a class of function which replaces placeholders with values in the template and writes to disk as config.php

The only thing to take care of is unique placeholders

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.