note 71079 deleted from function.mysql-connect by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 06:53:53 +0000 |
| Subject: | note 71079 deleted from function.mysql-connect by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196700@lists.php.net to get a copy of this message | ||
Note Submitter: Graham_Rule at ed dot ac dot uk
----
How to get at multiple MySQL databases from PHP while continuing to hide the user credentials in
Apache configuration files.
(This builds on my solution to the problem of hiding such credentials that I posted in May 2003 at
http://uk2.php.net/manual/en/function.mysql-connect.php#32035)
<Directory /var/www/html/multidatabase>
php_value mysql.default_user "username1 username2"
php_value mysql.default_password "secret private"
php_value mysql.default_host "localhost server.example.com"
</Directory>
Note that the quotes are necessary to prevent the parser complaining about seeing too many
parameters for php_value.
Given this setup in Apache, our script can fetch the composite value
$hostnames = @ini_get('mysql.default_host');
Split it into its component parts
$hostnames = preg_split("/[\s]+/", $hostnames);
Then use the values in this array as
if we had hard-coded:
$hostnames[0] = "localhost";
$hostnames[1] = "server.example.com"
Similar code may be written to fetch the usernames
and passwords.
(One 'gotcha' with the mysql_error() function is that it will not give a sensible error
report if there is a failure to open a second or subsequent connection. It uses the last
successfully opened connection as the basis for its message!)