Page 1 of 1

how to store database connection details?HOST,USERNAME,PASS

Posted: Sun Sep 11, 2005 7:44 am
by raghavan20
I used to pass database connection details from the same file I am running the script.
Now, I think that its not a good idea.
I am looking for suggestions where to store database connection details...like in a file???

Posted: Sun Sep 11, 2005 7:50 am
by feyd
first, I'd use a database abstraction class layer. This allows you to store the connection information there, or what I prefer, in a configuration file that is outside the webroot. A lot of hosts don't allow access outside the webroot to scripts however.

Posted: Sun Sep 11, 2005 9:41 am
by patrikG
Have a look at the registry pattern. Designed to store data in a globally accessible way.

Posted: Sun Sep 11, 2005 10:17 am
by jayshields
what feyd said is best, storing it in a php file outside of the web root, that is, if you have access to it.

Posted: Sun Sep 11, 2005 10:20 am
by Lobster
Even if you haven't got access to anything outside the webroot, you're still fairly safe with a php file so long as it's guaranteed to be parsed. Just to be sure (you never know what might happen if php gets damaged/reinstalled) I put a Deny from all on the file using .htaccess

Posted: Sun Sep 11, 2005 11:47 am
by Buddha443556
On shared servers, your trusting your neighbors. Basically, if one of your neighbors can guess the name and path of your config file it's theirs for the taking. Shared servers are not safe.

Having said that, Feyd's suggestion is best followed by Lobster's.

Posted: Sun Sep 11, 2005 3:27 pm
by sweatje
My situation may be different than most, but I will pass it on anyway in case someone can benefit from it. I have a number of databases (5-10) which I regularly connect to in various application. What I ended up doing was writing a parameterized Factory function into my database connection library. I happen to favor ADOdb as a database access layer, so by passing a string identifying which database I need to connect to, the Factory returns an ADOdb Connection already connected to the correct resource. If connection information changes (user names, hosts, etc.), I change them only in my standard db connection include and every application continues to work.

HTH

Posted: Sun Sep 11, 2005 7:32 pm
by timvw
A lot of Model classes use an instance of the DAO class. The DAO class is a wrapper around ADODB (some extra SQL generation etc..) and accepts a DSN string and the table name in the constructor..

Because, i don't want all my Model classes to call new DAO(.., ...) for maintenance issues (dsn changes or dao is replaced by DAO2) i have a Configuration class... This class is responsible to generate the DAO that a Model class requires..

Here is how that Configuration class looks like...

Code: Select all

/**
 * This class represents a Configuration container
 *
 * @author      Tim Van Wassenhove <timvw@users.sourceforge.net>
 */
class Configuration
{
    /**
     * Returns an appropriate DAO
     *
     * @param string $name the name of the DAO
     * @return DAO an instance of the DAO
     */
    function getDAO($name = '')
    {
        require_once(TVW_EXT . '/timvc/dao.php');
        $dsn = 'mysql://user:password@example.com/dbname';

        switch($name)
        {
                case 'messages':
                        $table = 'message';
                        $dao = new DAO($dsn, $table);
                        break;

                case 'feeds':
                case 'feedcontents':
                default:
                        $table = $name;
                        $dao = new DAO($dsn, $table);
                        break;
        }

        return $dao;
    }
}
And now implementations of a Model can use that as following:

Code: Select all

class Users extends Model
{
  function __construct()
  {
     $this->dao = Configuration::getDao('users');
     parent::Model($dao);
  }
}

Posted: Mon Sep 12, 2005 3:49 pm
by raghavan20
Thanks for replies guys. as far as now, I have a hosting company where I host my sites so I dont have access to documents out of wwwroot myself. I will store the database connection in one php file which will have a function and return an array of hostname, username & password.

Posted: Mon Sep 12, 2005 5:00 pm
by feyd
why not use a database abstraction layer so you don't have to care about the host, user and password? Just tell it to connect.. and it knows how.. :?