How do I format a PHP include() absolute (rather than relative) path?

5,232

Solution 1

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.

If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

If you want to set your own include "root", have a look at this question (specifically my answer of course :-)

Solution 2

You can just use include $_SERVER['DOCUMENT_ROOT'] . "/includes/analytics.php";

Solution 3

From the perspective of PHP root is the top of the file system on the web server, not the root from the perspective of the web browser.

Most people do one of the below.

Define a constant, in a global configuration file, and use that in each call to require/include.

Or they use code like this.

require_once realpath(dirname(__FILE__).'/config.php');
require_once realpath(dirname(__FILE__).'/lib/Database.php');

Using the environmental variables may be dangerous in some cases and be the source of security issues.

Solution 4

As @Stefan Mai says, PHP doesn't have a "root" path but you can define one quite easily - most sites have a page that's included every time (e.g. configuration file), to which you can add:

define('ROOT', dirname(__FILE__));

Then use include ROOT . '/includes/analytics.php';

Something that's also quite useful is the auto_prepend directive, which you can use in .htaccess on apache - not sure about setting it up on IIS (although you can have a global one in the PHP ini).

Solution 5

I combined the above suggestions and came up with this.

In my config.php file, I define ROOT as:

define('ROOT', $_SERVER['DOCUMENT_ROOT']);

Then I use it in my succeding files:

include_once(ROOT."/someFile.php");

include_once(ROOT."/includes/someFile.php");

Share:
5,232

Related videos on Youtube

Romowski
Author by

Romowski

Updated on July 09, 2022

Comments

  • Romowski
    Romowski almost 2 years

    How to check if user account locked or unlocked by ppolicy?

    Here is situation:

    1. I have added default ppolicy :

      dn: cn=default,ou=ppolicies,dc=scb,dc=kz
      objectClass: applicationProcess
      objectClass: pwdPolicy
      cn: default
      pwdAttribute: userPassword
      pwdLockout: TRUE
      pwdMaxFailure: 5
      pwdLockoutDuration: 900
      

    If ppolicy locked user for pwdLockoutDuration seconds (15 minutes) pwdAccountLockedTime operational attribute appears.

    It is ok!

    1. At this time user unable to authenticate within 15 minutes. Also I can detect if user is locked by checking if pwdAccountLockedTime attribute exists.

    It is ok too!

    1. After 15 minutes ppolicy unlocks user account and user can log in, but unless the user log in pwdAccountLockedTime attribute still exists.

    Is there any other way to find out if the user has been already automatically unlocked by ppolicy after lockout duration

  • Zack Peterson
    Zack Peterson over 15 years
    There's no way to use an absolute path?
  • Stefan Mai
    Stefan Mai over 15 years
    You can use an absolute path like "c:/includes/analytics.php" or "/home/iamnafets/includes/analytics.php", but PHP files aren't part of a "project" or "virtual directory" like ASP pages are.
  • Zack Peterson
    Zack Peterson over 15 years
    I wasn't specific enough. I want a single command that will work from either page.
  • gnud
    gnud over 15 years
    of COURSE php has a root. It's the filesystem root!
  • gnud
    gnud over 15 years
    You can change what directories PHP will search with the include_path directive. Have a look at stackoverflow.com/questions/344419/…
  • Jacco
    Jacco over 15 years
    This will cause problems when you move your site to another server.
  • jowie
    jowie over 12 years
    Doesn't seem to work on streamline.net servers either... Think they're Linux?
  • Siraj Alam
    Siraj Alam about 7 years
    Worked for me. Thanks a bunch. I wish I could upvote this multiple times.

Related