views:

494

answers:

3

strange problem with php on windows... my application loads a 'core' file that loads a settings file, registers autoloads, does initialization etc. at the top of the core file I have include_once("config.php"); this works fine for anything in the current directory, if I include the core file from a separate directory though it just silently ignores the include of the config file... has anyone seen this before?

/webroot/core.php

<?php
  require_once "config.php";
  //register autoloads
  //do some initialization... standard stuff
?>

/webroot/config.php

<?php
  define("WEB_ROOT","/webroot");
  define("DB_USER","root");
  // ... more stuff...
?>

/webroot/admin/index.php

<?php
  require_once("../dbconnect.php");
  echo WEB_ROOT;
  // print string literal WEB_ROOT rather than value in config.php
?>

My understanding is that file operations are relative to the directory of the file making the request, shouldn't the require_once("config.php") select the file relative to core.php? This code works just as I'd expect on a mac or linux but not at all on windows, (if I change the require to use the full path or ../, it works)

The real madness is that the require_once("config.php") doesn't throw any errors but none of the code inside is executed!

using xampp in vista

+1  A: 

Perhaps you're running into PHP's open_basedir restriction? To attempt to prevent some malicious scripts, PHP has a 'safe mode' that, among other things, limits where files can be loaded from. If xampp had this turned on by default you might have run into it.

You might also want to check your webserver's error logs to see if there are any indications of failure there (and turn on error reporting, preferably to E_ALL, until you get the issue sorted).

Amber
I did check ini to see if safemode was on, this isn't the case but great suggestion!Error reporting was set to just ignore Notices, but with a vanilla E_ALL I just get this:Notice: Use of undefined constant FILE_ROOT - assumed 'FILE_ROOT' in C:\webroot\core.php on line 87Not even a notice is raised on the unloaded config file!
Michael
+3  A: 

If PHP silently ignore errors, try to put at the top of file that is requested:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

Now all errors should be visible.

Then the best practice (according to me) is to in file that is requested (not in any of included files) define some constant with full filesystem path for application root. So in index.php if application root is /webroot/admin:

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

After that when you include something, use this constant. If the directory structure is:

/webroot/
    admin/
        index.php
    config.php
    core.php

And you want to include config.php into index.php:

require_once APP_DIR . '/../config.php';

In core.php to include config.php it would be:

require_once APP_DIR . '/../config.php';

And so on.

Using absolute filesystem paths will prevent any ambiguities.

Jakub Kulhan
using dirname(__FILE__) is a great way to define an absolute path! Unfortunately the application I'm working on sets this by hand in the config file and then uses this as the 'APP_DIR'. I really don't know why!
Michael
+1  A: 

If you want to include a file using a path relative to the file doing the inclusion, you'll have to prepend the full path, like this:

<?php
require_once(dirname(__FILE__) . "/config.php");
?>
Thorarin
not on nixes... its so strange that php just silently ignores the require in this case!
Michael
This line is actually from a script of mine that I developed specifically on Linux. The silent ignoring bit is indeed weird and I can't help you with that I'm afraid. I just posted what I adapted as a best practice years ago.
Thorarin