TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-26-2007, 08:20 PM   #1 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default Most efficient way of storing database configurations?

So i have 3-4 databases which I need to access within the same application. I know I should be storing the database configurations in a config.php file (which I have). The way I'll be accessing these is when I create a new database object, I'd like to specify which credentials to use.

So essentially, I'd like to be able to do something along these lines:
PHP Code:
$serverName DatabaseFactory::factory('mysql'$databaseName); 
Or if someone knows of a better way, let me know.

Thank you. :)

Last edited by bdm : 11-26-2007 at 08:24 PM. Reason: <3
bdm is offline  
Reply With Quote
Old 11-26-2007, 08:52 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

ok, im guessing all you DB's are on the same server?

In the factory have the function mysql_select_db(), that is provided they are on the same server, thus the link identifier (created from mysql_connect function) will work.

Just something i thought of off the top of my head.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 11-26-2007 at 08:53 PM. Reason: clumbsy fingers
sketchMedia is offline  
Reply With Quote
Old 11-26-2007, 08:58 PM   #3 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I don't follow.

My config.php file also has to have the info (username, password, ...) to connect to several databases.

And not all databases are located on the same server. ;)
bdm is offline  
Reply With Quote
Old 11-26-2007, 09:23 PM   #4 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

right, then i think you will need a new link identifier then, my original thought was that you have the same server but with different DB's, then my example would work.

ill have a think about that, need to find a source of coffee to get my brain working.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-26-2007, 09:27 PM   #5 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

;)

I've played with the Zend Framework for a bit. And the way it seems to handle it is by putting the their configuration settings into an array, then loading it into the registry. Then when you need a connection, you simply use the registries get method.
bdm is offline  
Reply With Quote
Old 11-26-2007, 09:31 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Why not load your config settings into a multi dimensional array?
For example:
PHP Code:
$serverName DatabaseFactory::factory('mysql'$databaseName); 
inside your factory:
PHP Code:
$configArray[$databaseName]['host'];
$configArray[$databaseName]['user'];
$configArray[$databaseName]['pass']; 
etc.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-26-2007, 09:32 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I have like this...

php Code:
class DBmysql implements iDB {
       
        private $host;
        private $user;
        private $pass;
        private $data;
        public $table = array();
        public $col = array();
       
       
        public function setHandler($host, $user, $pass, $data) {
           
            $this->host = $host;
            $this->user = $user;
            $this->pass = $pass;
            $this->data = $data;
           
            return $this;
           
        }
       
        public function connect() {
           
            @mysql_connect($this->host, $this->user, $this->pass) or die("Could not connect to the database.");
           
            return $this;
           
        }
       
        public function select() {
           
            @mysql_select_db($this->data) or die("Could not select database.");
           
            return $this;
           
        }

etc....

So you just do like this to call it:

php Code:
$tanaxia['database'] = DB::getInstance($tanaxia['config']['database']['type'])
$tanaxia['database']->setHandler(
               
                        $tanaxia['config']['database']['host'],
                        $tanaxia['config']['database']['user'],
                        $tanaxia['config']['database']['pass'],
                        $tanaxia['config']['database']['data']
                       
                        )
                        ->connect()
                        ->select();

All those variables are set in the config.php

And what you can do then, is to make a script that loads the database name from a .txt file

And then another script that allows you to change that value.

So your $tanaxia['config']['database']['data'] would be the value of the string in the .txt file ;)

Piece a cake :) or not xD
But that's the idea anyways.
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
Wildhoney (11-30-2007)
Old 11-28-2007, 02:23 PM   #8 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Sounds good, I'll put them in associative arrays. :)
bdm is offline  
Reply With Quote
Old 11-28-2007, 05:14 PM   #9 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
And what you can do then, is to make a script that loads the database name from a .txt file

And then another script that allows you to change that value.

So your $tanaxia['config']['database']['data'] would be the value of the string in the .txt file ;)
Puting the database name or any database information in any file that is accessible to users somehow is not a good idea.

If you put the database name or any information like that in a text file make sure with .htaccess (for apache) or otherway (for other servers) that the file is not accessible to users.
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-28-2007, 05:54 PM   #10 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

My config files are not in the root directory, so that shouldn't be a problem. And I always make sure to lock those directories with .htaccess. :)
bdm is offline  
Reply With Quote
Old 11-28-2007, 05:59 PM   #11 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by gcbdm View Post
My config files are not in the root directory, so that shouldn't be a problem. And I always make sure to lock those directories with .htaccess. :)
Oh, if you make sure the files are locked then it's ok
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 11-28-2007, 10:37 PM   #12 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

alternatively just name your config file with the .php extension, then the contents is hidden from the user.
But if the config is outside the root and locked then it should be ok i reckon
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 11-29-2007, 10:26 AM   #13 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

Maybe you could get some use for the function:
http://se.php.net/manual/sv/function.parse-ini-file.php
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 11-29-2007, 02:57 PM   #14 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

One downside with the INI file approach is that if somebody directly visits that file, it won't be parsed as PHP and thus show the MySQL credentials in all their beauty. You will need to be quite up with the htaccess file configuration in order to consider such an approach, that way you can prevent access to the INI file.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-29-2007, 04:58 PM   #15 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
One downside with the INI file approach is that if somebody directly visits that file, it won't be parsed as PHP and thus show the MySQL credentials in all their beauty. You will need to be quite up with the htaccess file configuration in order to consider such an approach, that way you can prevent access to the INI file.
True.

Guess in that case you could tell your htaccess file to redirect any requests to *.ini . :)
bdm is offline  
Reply With Quote
Old 11-29-2007, 09:22 PM   #16 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yep. The best technique is to pretend as though the file does not exist. Use the same type of request, such as 301 move, regardless of whether or not they've found the correct config file, or a file that doesn't exist. That way they will never know, unless of course they crack your code - which still makes it tougher, if they've found a config file or not.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-30-2007, 12:18 PM   #17 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

WildHoney, It's like you read my mind because that it exactly how I was going to approach it. :)
bdm is offline  
Reply With Quote
Old 11-30-2007, 12:30 PM   #18 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This is good! Just ensure that you use the same kind of redirect. That will only matter if somebody is determined to crack your code as they will need to read the HTTP headers to deduce the type of redirect. There are a whole array of redirects - such as 301 redirect is the one most people will be aware of if they've touched upon SEO before, but it doesn't matter which one you choose as long as you're consistent.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-30-2007, 03:16 PM   #19 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Using a 301 means that the resource does exist but it is to be found at another location. I'd prefer a 404 (not found) error to be produced.
Salathe is offline  
Reply With Quote
Old 11-30-2007, 03:33 PM   #20 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

A 404 is not bad if you customise your errors, but if you remain consistent with whichever you decide to go for, then you'll have no problems. Redirecting would simply redirect back to the home page.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design