Database Connections Outside of Webroot

For extra security many often store their database connection credentials outside of the web root. This is fairly easy to achieve without to much work.

For those of you who are unsure how to store database connection credentials in a separate file please refer to my article on Connecting to MySQL with PHP

Once you have your connections in a php file (this is also relevant to other languages such as asp etc) you need to store it somewhere on your server outside of the web root. In this example we will call the file MyCat.php.

Linux Systems

Put this file outside of the web root eg in /home/myuser/ the web root for example (where all your web files are stored) may well be /home/myuser/htdocs/

Now to include this file into your script use the following:-

include("/home/myuser/MyCat.php");

This will call the file from outside of the webroot.

Windows Systems

This is very similar to above. Place the MyCat.php file in the root of the C drive in a directory called MyInclude eh C:MyInclude

Now to include this file into your script use the following:-

include("C:MyIncludeMyCat.php");
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *