Include errors

Hi, I am building my first website in PHP and am having some issues with include files.

Thus far I have done all my dev locally and use the following to find my include files:

include_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/magicquotes.inc.php’;

The path works fine when I run the site locally.

However, when I run the site on my web host’s servers I get this message:

Warning: include_once() [function.include]: Failed opening ‘/usr/local/apache/htdocs/includes/magicquotes.inc.php’ for inclusion (include_path=‘.:/usr/lib/php:/usr/local/lib/php’) in /home/a6375238/public_html/index.php on line 3

Being the noob that I am, I am very confused as to why a local path is being referenced. I assume I am ignorant to some config somewhere. Any advice would be greatly appreciated.

Thanks.

Huh, that’s odd. If you just upload .php files to your server there should be no reference to your local environment. Is there a database involved here or something like that?

Only case can be (or can happen accidentally sometimes) that $_SERVER[‘DOCUMENT_ROOT’] has been reset (assigned some other values) somewhere in your system/php files above the line. Is there any caching mechanism in your code. I don’t think so but that might be another case. So the problem might due to:

  • Assigned a value in $_SERVER[‘DOCUMENT_ROOT’] somewhere above the use.
  • File or the folder has not been uploaded correctly in the correct path.
  • Some caching mechanism working.

Better try this way then:


include_once dirname(__FILE__) . '/includes/magicquotes.inc.php';

I tend to do this:


// Somewhere in your global script/init script or whatever, before any includes
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));

Then when including scripts


require_once(CWD . '/includes/scripttoinclude.php');

Basically set the current working directory (CWD) as a constant and then the includes will be running from the current path.

Ok, this gets a little stranger.

In order to limit the problem to one variable

I just did the following echo

<?php
echo $_SERVER[‘DOCUMENT_ROOT’];

?>

I got this:

/usr/local/apache/htdocs

There is only this one file on the server. It has to be a config issue, right?

It’s a consequence of being on a shared host. Just use:
include_once ‘includes/magicquotes.inc.php’;
Later on you can learn how to set your include path so your app can live outside of you web directory.