0

I want to start using configuration.php file, like joomla or wp is using too to store some almost static variables like db logins, theme id or selected language. Problem is, that I dont know how to handle with updating this values. Is there possibility how to edit this variables separately, or I have to always replace whole content of the file? Lets say, that I have config.php file with content like:

class DB {
    public $theme = 1;
    public $db_host = 'abc';
    public $db_user = 'adsabc';
    public $db_pwd = 'dsads';
    public $db_charset = 'utf8';
    public $lang = 'gb';
    public $debug= 0;
    public $gzip = 0;
    }

And I want to change variable $lang = 'gb'; to $lang = 'de'; What is most effective way to do this with php?

3
  • Why not just do a single include/require for that file and include/require it inside all your files, that way you will only have the "one" file to modify. It can be done, but that would require a bit of PHP magic ;-) Commented Jan 22, 2015 at 20:36
  • 1
    Generally config files like this would be edited by hand. For non-static values which should change without "re-starting" the application you'd use a database. (Or a structured data file, like XML or JSON. But there are concurrency concerns with files.) You can of course set the values directly in code, but that wouldn't change what's physically persisted in the file so the next time it's read it would be the static value. Reading/writing code files from code is... a can of worms you probably don't want to open. Commented Jan 22, 2015 at 20:37
  • If you're going to use a JSON method, make absolutely certain that you protect that file securely. Commented Jan 22, 2015 at 20:39

2 Answers 2

2

You can write a PHP script that will look for and replace those values, but parsing code with regex or similar would be a bad idea.

Instead, you'd be better to implement a JSON file or something similar which you could store your values in, e.g:

{
    "lang": "gb",
    // everything else
}

Then modify your configuration file to parse that file into variables:

private function parseConfig()
{
    $json = json_decode(file_get_contents('yourconfig.json'));
    $this->lang = $json->lang;
    $this->anotherVar = $json->anotherVar;
}

This way, reading and writing variable data to a contained file is much more manageable than modifying a PHP file (introducing tenfold ways you can break your configuration file, which is the backbone of your site!).

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

2 Comments

I'd imagine there'd be some form of security issue should that expose the OP's login credentials, should it contain them or is that just for the "lang"?
In theory, having credentials in a file is no different to hard coding them in PHP provided you set the right permissions on the server to prevent prying eyes. Edit I'd add that if the user has access to more on the server than just the web root, might be safer to just hide the JSON file outside the webroot somewhere.
2

You may have some success with var_export(). You'll probably need to eval() it to read it (make sure you do this inside of a function scope), then file_put_contents() the output of var_export. Then it will be editable by hand. It may or may not look exactly as you want it to, though.

2 Comments

var_export would work just fine with an array. prepend '<?php $config=' to the string output of var_export and the saved file simply be includeed
@Steve is right. I'm still not sure it's how I would do it, though. Would not be my first choice, though I've serialized data as code and been surprised that it's faster than serialize sometimes.

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.