如何为 INCLUDE 和 REDIRECT 创建绝对路径,无论目录嵌套级别如何?

发布于 2024-12-06 13:39:53 字数 711 浏览 1 评论 0原文

我有一个与这里提到的问题类似的问题,除了我需要绝对路径的解决方案:

如何使用 PHP 包含具有相对路径的多个目录/子目录

我的设置中的附加位是我有多个文件(“config.php”,“functions.php”等),我将其包含在“header.php”文件中,然后仅在我的 sub-sub-sub-sub-etc 文件夹项目中包含“header.php” 。基本上,头文件显示“如果未登录,则重定向到顶级index.php 文件”。然而,标头本身位于其自己的子文件夹中,名为“common_inc”。当我需要从任何非顶级目录执行重定向(在标头中管理)时,我遇到的问题就会出现。

我想到的唯一解决方案是 1) 将“allow_url_include”设置为 true 或 2) 将所有文件放在平面目录结构中并从那里开始工作或 3) 从标头中删除重定向并手动在所有文件中放置绝对路径重定向文件。显然所有这些解决方案都是不可取的。那么,任何人都可以想出一种更好的方法来建立不会根据请求位置而改变的绝对路径(对于包含和重定向)吗?

我确信我忽略了一些非常简单的事情,但我的大脑相当糟糕。

提前致谢!

I have a problem similar to the one mentioned here, except I need a solution for ABSOLUTE PATHs:

How to use a PHP includes across multiple directories/sub directories with relative paths

The additional bit in my setup is that I have multiple files ("config.php","functions.php",etc) that I'm including in my "header.php" file, then including only "header.php" in my sub-sub-sub-sub-etc folder items. Basically the header file says "if not logged in then redirect to the top level index.php file". The header itself is, however, in it's own subfolder called "common_inc". The problem I'm experiencing comes in when I need to perform a redirect (which is managed in the header) from any non top-level directory.

The only solutions that come to mind are to 1) set 'allow_url_include' to true or 2) place all files in a flat directory structure and work from there or 3) remove the redirect from the header and manually place an absolute path redirect in all files. Obviously all of these solutions are undesirable. So can anyone think of a better way to establish ABSOLUTE PATHS (for both includes and redirects) that won't change based on their request location?

I'm sure there is something amazingly simple I'm overlooking, but my brain is rather fried atm.

Thanks in advance!

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

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

发布评论

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

评论(1

尛丟丟 2024-12-13 13:39:53

dirname 函数在这里可能有用。它返回当前目录的路径名。
目录名 - PHP 手册

The dirname function may be of use here. It returns the pathname for the current directory.
dirname - php manual

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