0

I am having trouble trying to get a file to be included into a php script. This file is stored in the root folder, which is not the actual web root, but the root directory that I have access to via the shared hosting account.

I have created a folder in this directory that stores php session data, the .htaccess configuration regarding this would be the following line:

php_value session.save_path "/usr/home/accountname/sessions"

The web root would be stored in this directory:

/usr/home/accountname/public_html/domainname/

What I am trying to do is secure the scipt that holds database connection details, in a folder that would be something like this:

/usr/home/accountname/includes/

This is how i currently include the file:

require_once $_SERVER['DOCUMENT_ROOT'].'/includes/config.php';

I have tried this:

require_once $_SERVER['PATH'].'/usr/home/accountname/includes/config.php';

As $_SERVER['PATH'] points to /bin

This is not working as the page comes out blank, this would mean that the variables defined in config.php are not active as the page has not been included.

Could anyone provide some information regarding this matter, or point me in the direction of solving the problem. Also, any tutorials or other info regarding this would be greatly appreciated.

Thank you!

2 Answers 2

3

If the script including config.php is stored under your Web root, you can write:

require_once dirname(__FILE__) . '/../../includes/config.php';

Have you already tried?

2
  • No I have not tried this, but the way I see it, the directory implied would be based on the script's path? I would essentially not want to rewrite this depending on each script path. Would I include a file, like config.php that is in my webroot, then from that file include the actual config that is below the webroot? Oct 9, 2011 at 7:05
  • 1
    This is why you should have a front controller. Try using this: require_once $_SERVER['DOCUMENT_ROOT'] . '/../../includes/config.php';. Oct 9, 2011 at 7:08
1

If your script is in /usr/home/accountname/public_html/domainname/something.php, you could use this:

require_once(dirname(dirname(dirname(__FILE__))).PATH_SEPARATOR."includes".PATH_SEPARATOR."config.php");
2
  • Thank you for the reply, but the actual script i would be trying to include would be at this directory: /usr/home/accountname/includes/something.php Could you briefly explain what you are accomplishing with that? Oct 9, 2011 at 6:29
  • @BlackberryFan: Yes, that should be what this is doing. dirname returns the name of the directory holding the file or directory given to it (so foo/bar/baz -> foo/bar and foo/bar -> foo). __FILE__ is the path of the currently-executing file. So if you start from /usr/home/accountname/public_html/domainname/something.php, we take the dirname three times: /usr/home/accountname, and then we add /includes/config.php, taking into account the PATH_SEPARATOR (since Windows uses `, most other OSes use /`).
    – icktoofay
    Oct 9, 2011 at 21:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.