12

I have an config.php file where I simply make an huge array that contains all the framework configuration. Also the database source string thing like "mysql:host=localhost;dbname=mydb" (whats that called, btw?) and username + password for DB. I'm afraid this is:

  1. stupid
  2. not good; better solution there
  3. not secure (?)

so how do the PHP experts do that?

4
  • 1
    I'd say it's called a DSN. en.wikipedia.org/wiki/Database_Source_Name Commented Dec 27, 2009 at 12:35
  • 1
    "connection string" is a bit more generic than DSN. Commented Dec 27, 2009 at 12:41
  • dont call it 'config.php', its the first name some malware will look for. Call it hey-this-is-just-a-useless-file-i-swear-man.php or something like that ;) Commented Jan 8, 2010 at 21:48
  • See also: stackoverflow.com/questions/1650629/… Commented Jan 8, 2010 at 22:38

7 Answers 7

12

If you have a www, httpdocs or public_http folder or something like that, where your php application is situated, then it is good practice to put the config file outside of that folder, and just access it like this:

include "../config.php";

Nobody can gain access to that file without FTP access, and so it's relatively safe compared to having it in the application folder.

If you don't have such a folder, you can create one, and make a .htaccess file in the root, which redirects all requests to that folder. There are many different ways to do that, but that's a different question all together.

Sign up to request clarification or add additional context in comments.

4 Comments

unfortunately, almost 99,99% of all cheap virtual hosters don't provide any such directory. Everything from within the root is accessible, and there's no ftp access to what's above the web root.
Actually, most 'cheap' hosts I've had, have provided such a directory in some form or another. It's also in the host's interest to do so. But see my edit for options.
@openfrog: quite a statement you have there, where do you get your data from?
all the cheap virtual hosting i've ever seen has this option.
5

That's how most do it, but you could also try some of these solutions:

  • Save the configuration file outside the web folder (this requires that the open_basedir configuration in PHP is disabled).
  • Restrict access to the configuration file by using .htaccess:

    <Location /config.php> Order deny,allow Deny from all </Location>

  • Use .ini files and the parse_ini_file function (this is not really a solution in itself, but could be combined with the others)

Comments

5

I store it in a plain text ini style configuration file, usually above the web root so as not to allow users access to it. In the cases where it is accessible, I usually have a .htaccess file with deny from all so as to prevent all access to it.

Storing it in a PHP file accessible to users should be fine, but it isn't ideal. If the sever handles PHP files correctly, even if people can access the file, they can't access the values as they just get the output (nothing). There are of course issues with this, (see comments).
Using PHP files is the most common method with PHP projects (Both FOSS and commercial) I have used. Most of them didn't both storing them above the web root. With any stable setup, there is on the face of it very little point in storing your configuration file above the web root, although given Murphy's law it is worth doing if you can (That or use .htaccess or the equilivent for your server to deny user access to a directory)

5 Comments

"Storing it in a PHP file should be fine." - Unless yet another webserver software update "destroys" the configuration esp. the .php->application/x-httpd-php mapping...again. This has happened before (at bigger hosting companies) and suddenly all .php file were sent as text/plain :-S
one reason why PHP most certainly sucks ;) but there's no real alternative. so we can start obfuscating our code, huh? ;)
That has nothing to do with PHP per se, but with how many servers are configured. Obfuscation will help neither, as the password needs to be there in some readable form to use it for opening a connection.
@openfrog: You seem to ask a lot of rather basic questions for someone that seems to dislike PHP so much.
Naming your PHP file something that begins with .ht (for instance .htconfig.inc.php) also helps, since Apache usually has a rule in its main config file to never serve any files that are named .ht*
3

Why storing DB username & password in "config.php" is not fine? as long as i know, the data in this file can't be shown publicly.

E.G.

<?php
$DB_User = "amindzx";
$DB_Pass = "Something";

// connect to DB and so on.
?>

unless if the hacker can gain access to your FTP.

Comments

1

Why it's stupid to hold simple config.php file without any securities? Even if programmer finds this file he can do nothing, because, like amindzx said "this file can't be shown publicly." Or I'm wrong?

4 Comments

Basically, no, but the server's PHP configuration could change and this could lead to the file becoming visible after all.
"but the server's PHP configuration could change", can you give an example?
There are some cases of apache not working correctly and serving php files as plain text. This hasn't happen to me, but I have heard stories of this.
I've seen it often enough, they change some config setting, or are in the process or rebuilding their system after a crash, and Apache is running without mod_php. Hello source code to everyone...!!
1

With sensitive info like database or payment gateway credentials, and when I have control over the server, I like to add lines like the following to my apache virtual host config:

SetEnv DB_USER "myuser"
SetEnv DB_PASS "mypass"

In your PHP, you can access these using $_SERVER['DB_USER']. You can make this config file readable only by root, which you can never do to a file that php accesses at run time. One caveat: Be sure you disable php_info and don't expose these variables with something silly like print_r($_SERVER). (Much of this is paraphrased or stolen from here.)

For non-sensitive configuration, I like to do a class full of constants, which is similar to your setup, but I like the OOP-ness of it.

class Application
{
    const CONTACT_EMAIL = "[email protected]";
}

usage:
$contactEmail = Application::CONTACT_EMAIL;

1 Comment

This is the best solution for shared hosting.
0

I usually store settings in a config.php too, such as database connection settings, file paths etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.