PHP 包含 Mod_Rewrite 的路径

发布于 2024-09-29 23:50:20 字数 515 浏览 0 评论 0原文

我一直在尝试找到一种“简单”的方法,将受 mod_rewrite 地址更改影响的文件包含在我的 PHP 文档中。

目前我们的 config.php 文件位于 public_html/lib/config.php 中,我尝试做的是 include('lib/config.php') 但它在页面上出错已通过 mod_rewrite 更改了其地址。

例如,“http://www.example.com/user-profile.php?user=123”将使用 include('lib/config.php') 工作

,但

“http://www. example.com/user/123”使用 include('lib/config.php') 无法找到该文件。

有没有办法为 PHP 文件设置默认包含路径,以便无论页面位于/重写的位置如何 include('lib/config.php') 都可以工作。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2024-10-06 23:50:20

包含不受 mod_rewrite 影响。你的问题一定出在其他地方。

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

ペ泪落弦音 2024-10-06 23:50:20

使用完整路径,这一定有帮助......
像这样

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

(但这很奇怪,重写不会弄乱路径,只是不能像那样工作,可能错误在其他地方)

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)

长亭外,古道边 2024-10-06 23:50:20

首先,请务必检查其他人提到的有关 mod_rewrite 的问题。

但要回答您的具体问题:有两种常见的方法可以处理此问题。

1) 您可以将 include 设为文件的完整路径。无需过多讨论,如果您要使用 APC,这是特别建议的。这就是我在所有应用程序中所做的事情。

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

2)您可以添加修改php包含路径。这可行,但将路径添加到包含路径时会产生性能影响。

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

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);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文