views:

37

answers:

3

Hi there,

I have been trying to find an 'easy' way of including files in my PHP docs that are subject to mod_rewrite address changes.

At the moment our config.php file is located in public_html/lib/config.php, and what I have tried to do is include('lib/config.php') but it errors on pages that have had their address changed by mod_rewrite.

E.g. "http://www.example.com/user-profile.php?user=123" will work using include('lib/config.php')

but

"http://www.example.com/user/123" using include('lib/config.php') fails to find the file.

Is there any way to set a default include path for PHP files so include('lib/config.php') works regardless of where the page is located / rewritten.

Thanks

A: 

Use full path, that must help... Like this

include dirname($_SERVER["SCRIPT_FILENAME"]) . DIRECTORY_SEPARATOR . 'lib/config.php';

(But that's strange, rewrite souldn't mess with the path, it's just don't work like that, probably error is somewhere else)

Vladson
+1  A: 

Includes are not affected by mod_rewrite. Your problem must lie somewhere else.

nikic
A: 

First, make sure to check the issues about mod_rewrite that others are mentioning.

But to answer your specific question: There are two common ways to handle this.

1) You can make the include be the full path to the file. Without going into too much detail, this is especially advisable if you are going to be using APC. This is what I do in all my applications.

<?php
define('BASE_DIR', '/path/to/includes');
include BASE_DIR . "/lib/config.php";
?>

2) You can add ammend the php include path. This works, but there are performance implications when adding paths onto the include path.

<?php
$path = '/path/to/public_html';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
?>
sfrench