Link to home
Start Free TrialLog in
Avatar of Cesar Aracena
Cesar AracenaFlag for Argentina

asked on

Read Config:: values from another file

Hello guys,

I have the following script that I include above my header (so it's called from everywhere in the site) to handle my PDO connection to the DB:
  Config::write('db.host', '127.0.0.1');
  Config::write('db.port', '3306');
  Config::write('db.basename', 'mydbname');
  Config::write('db.user', 'mydbuser');
  Config::write('db.password', 'mydbpassword');

  class Config {

    static $confArray;

    public static function read($name)
    {
      return self::$confArray[$name];
    }

    public static function write($name, $value)
    {
      self::$confArray[$name] = $value;
    }

  }

  class Core {

    public $dbh; // handle of the db connexion
    private static $instance;

    private function __construct() {

      // building data source name from config
      $dsn = 'mysql:host=' . Config::read('db.host') .
             ';dbname='    . Config::read('db.basename') .
             ';port='      . Config::read('db.port') .
             ';connect_timeout=15';
      // getting DB user from config                
      $user = Config::read('db.user');
      // getting DB password from config                
      $password = Config::read('db.password');

      $this->dbh = new PDO($dsn, $user, $password);
      // $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }

    public static function getInstance()
    {

      if (!isset(self::$instance))
      {
        $object = __CLASS__;
        self::$instance = new $object;
      }
      return self::$instance;

    }

    // others global functions
  }

Open in new window

What I would like to know if is I can get all that DB connection information like username and password and store it in another file above my root directory so it never becomes accesible no matter what goes wrong.

So the two questions are: How should I write that other file and then how would I call it using the real path within my server?

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cesar Aracena

ASKER

Thanks for taking the time Ray. As always a very detailed response. You are right. If everything is configured like it should, no one will ever be able to see my files.