0

I can't seem to get this file to open in php:

My folder structure is as follows:

/home/www/site/user/index.php

/home/www/site/lib/login.inc.php

I am trying to open login.inc.php in the user index.php file:

if (!defined('SITE_ROOT') || !defined('USER_ROOT') )
{
    define('SITE_ROOT', '../');
    define('USER_ROOT', './');
}

require_once SITE_ROOT . 'lib/login.inc.php';

But this keeps coming up with a failed to open stream error.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
jonnnnnnnnnie
  • 1,219
  • 3
  • 15
  • 24

1 Answers1

-1

require_once resolves relative directories with respect to the working directory. When the webserver executes a PHP script in response to a HTTP request, the working directory is the directory of that PHP script. When that PHP script includes some other PHP script, the other PHP script still has the original directory set as working directory.

This means it is irrelevant, where the included file is located relative to the including file. To compensate for that, you can use absolute paths in include-statement. In /home/www/site/user/index.php, do

require_once dirname(__FILE__) . '/../lib/login.inc.php'
Oswald
  • 31,254
  • 3
  • 43
  • 68