0

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

2
  • 3
    mod_rewrite should have no effect on inlude paths (because these paths are not processwed as HTTP requests). Commented Oct 29, 2010 at 16:13
  • What @Mchl says. If the PHP file is the same, the path will be the same Commented Oct 29, 2010 at 16:18

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.