I would like to automatically override my Joomla configuration based on the host I am using. For example:

$servername = getenv("HTTP_HOST");
if($servername == "localhost") {
    JConfig::host = 'localhost';
    JConfig::user = 'root';
    JConfig::password = 'pass';
    JConfig::db = 'myproject';

what is the best way to do this? (The code above does not work)


its not the good idea to modify the config file but here is one way to do this

$servername = getenv("HTTP_HOST");
if($servername == "localhost") {
    $host = 'localhost';
    $user = 'root';
    $password = 'pass';
    $db = 'myproject';

//now use it in the config file like this 
define('DB', $db);
define('DBHOST', $dbhost);
define('USR', $usr);
define('PWD', $pwd);

class JConfig {

    public function __construct(){
            $this->host = DBHOST;
            $this->user = USR;
            $this->password = PWD;
            $this->db = DB;
    public $host;
    public $user;
    public $password;
    public $db;

I hope it will help you

  • Thank you, this helped me a lot - I have simplified it a bit - I put my server condition directly into the constructor, so I don't need variables and defines.
    – Gerfried
    Sep 3 '16 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.