php 相对路径和目录

发布于 2024-12-10 18:39:38 字数 2100 浏览 0 评论 0原文

我想知道一件事,但我似乎找不到一个好的、明确的答案,甚至找不到解决此问题的方法:

我的 PHP 网站具有以下结构:

root
   functions
   generators
   helpers
   scripts
   style
   index.php

这些都是文件夹和一个 php 文件。 Functions 包含一堆与数据库连接和各种其他数据库操作(例如插入、删除、更新等)相关的 op php 文件。 Generators 包含自动生成网页的类,使它们看起来都一样。助手是处理登录、注销、注册等的类。脚本是 javascript,样式是 CSS。

在我的生成器文件夹中,有一个文件 mainGenerator.php,它会生成网站的各个部分:

 private function generateLogin()
    {
        if (!isLoggedIn()) {
            echo "
                <h2>Login</h2>
                <form method='post' action='../helpers/login.php' id='loginForm'>
                <p>
                Username:<br/>
                <input class='search' type='text' name='username'/>
                Password:<br/>
                <input class='search' type='password' name='password'/>
                <input name='login' type='image' style='border: 0; margin: 0 0 -9px 5px;' src='style/login.png' alt='Login' title='Login'/><br/>
                No account yet? <a href='../register.php'>Register</a>
                </p></form>";
        } else {
            echo "
                <h2>Welome, <a href='user.php' style='color: #1293EE;'>" .
                 $_SESSION['user_name'] .
                 "</a></h2>
                <a href='logout.php'>Log off</a>";
        }
    }

这段代码在每个页面上生成登录框,或者如果用户已经登录则显示欢迎消息。如您所见,该操作引用“../helpers/login.php”,因为从该生成器的角度来看,这是登录帮助程序的相对位置。

现在,问题是:如果我按下index.php上的登录按钮(http://example.com /ProjectName/index.php)它将我重定向到 http://example.com/helpers/ login.php 并表示找不到该文件。

我明白问题所在了,post 操作看到来自 index.php 的请求,并向上一个目录查找,然后查找 /helpers/login.php,当然该目录不存在。

解决方法可能是将操作更改为,

action='/helpers/login.php'

但是如果我需要从某个子目录中访问generateLogin(),这会给我带来同样的问题... 如何解决这个特定问题,即:无论我尝试从哪里访问它,对 /helpers/login.php 的引用都保持正确。

I'm wondering something, but I can't seem to be finding a good, clear answer, or even a solution to this problem:

My PHP website has the following structure:

root
   functions
   generators
   helpers
   scripts
   style
   index.php

These are all folders and one php file. Functions contains a bunch op php files related to database connection and various other database operations such as inserting, deleting, updating,... Generators contains classes to automatically generate web pages to make them look all the same. Helpers are classes that handle login, logout, register, etc. Scripts are javascript and Style is CSS.

In my generators folder, there is a file mainGenerator.php, this generates various pieces of the website:

 private function generateLogin()
    {
        if (!isLoggedIn()) {
            echo "
                <h2>Login</h2>
                <form method='post' action='../helpers/login.php' id='loginForm'>
                <p>
                Username:<br/>
                <input class='search' type='text' name='username'/>
                Password:<br/>
                <input class='search' type='password' name='password'/>
                <input name='login' type='image' style='border: 0; margin: 0 0 -9px 5px;' src='style/login.png' alt='Login' title='Login'/><br/>
                No account yet? <a href='../register.php'>Register</a>
                </p></form>";
        } else {
            echo "
                <h2>Welome, <a href='user.php' style='color: #1293EE;'>" .
                 $_SESSION['user_name'] .
                 "</a></h2>
                <a href='logout.php'>Log off</a>";
        }
    }

This piece of code generates the loginbox on every page, or displays a welcome message if the user is already logged in. As you can see, the action references to "../helpers/login.php", because that's the relative location of the loginhelper from this generator's point of view.

Now, here's the problem: If I press the login button on the index.php (http://example.com/ProjectName/index.php) it redirects me to http://example.com/helpers/login.php and says the file isn't found.

I see what the problem is, the post action sees the request coming from index.php and goes up one directory and then looks for /helpers/login.php, which doesn't exist of course.

A fix might be to change the action to

action='/helpers/login.php'

but that gives me the same problem should I need access to generateLogin() from within a certain subdirectory...
How can this particular problem be solved, i.e: that the reference to /helpers/login.php stays correct, no matter from where I try to access it.

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

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

发布评论

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

评论(2

下壹個目標 2024-12-17 18:39:38
action='/ProjectName/helpers/login.php'

假设您的根目录是 http://localhost 那么上面的链接应该始终解析为 http://localhost/ProjectName/helpers/login.php 是否从 http://localhost/ProjectName/ 调用index.phphttp://localhost/ProjectName/subdir/index.php

如果您不想将“ProjectName”硬编码到许多不同的脚本中,您可以使用全局变量并在配置文件中定义它:

helpers/ConfigOptions.php:

<?php
$ProjectName = "ProjectName";
?>

然后在您的脚本中,包含配置文件并使用您定义的变量:

index.php :

include $_SERVER['DOCUMENT_ROOT'] . '/helpers/ConfigOptions.php';

...

echo "
  <form action='/$ProjectName/helpers/login.php'>
  ....
";

更多关于DOCUMENT_ROOT:

DOCUMENT_ROOT 在服务器配置中定义文件,是执行脚本的文件系统的根目录,而不是您在浏览器中键入的网址。在上面的示例中,我假设文档根目录类似于 /home/user/www/ProjectName。如果 DOCUMENT_ROOT 仅是 /home/user/www 那么您可以将包含路径更改为此:

include $_SERVER['DOCUMENT_ROOT'] . '/ProjectName/helpers/ConfigOptions.php';

或使用相对路径。我会投票给后者,因为您不需要对“ProjectName”进行硬编码。

因此 include() 和 require() 将采用:

  1. 文件的绝对路径
  2. 相对于当前执行脚本的路径。如果A包含B且B包含C,则当B包含C时,它需要提供相对于A的路径。

那么什么时候需要使用DOCUMENT_ROOT呢?通常,模板中的相对路径(例如../helpers/file.php)可以解析为不同的绝对路径,具体取决于文件包含的内容。在上面的示例中,index.php 包含 ConfigOptions.php,您可能不需要使用 DOCUMENT_ROOT,因为 index.php 不是模板。无论如何,为了安全起见,我使用了它,但也许我在这里打开了一整罐蠕虫。我希望我没有让你更加困惑。

action='/ProjectName/helpers/login.php'

Assuming your root is http://localhost then the above link should always resolve to http://localhost/ProjectName/helpers/login.php whether it's called from http://localhost/ProjectName/index.php or http://localhost/ProjectName/subdir/index.php

If you don't want to hardcode "ProjectName" into many different scripts, you can use a global variable and define it in a config file:

helpers/ConfigOptions.php:

<?php
$ProjectName = "ProjectName";
?>

Then in your scripts, include the config file and use the variable you defined:

index.php:

include $_SERVER['DOCUMENT_ROOT'] . '/helpers/ConfigOptions.php';

...

echo "
  <form action='/$ProjectName/helpers/login.php'>
  ....
";

More about DOCUMENT_ROOT:

DOCUMENT_ROOT is defined in the server configuration file and is the root on the filesystem of where scripts are executing, not the web address you would type in a browser. In the above example, I'm assuming that document root looks something like /home/user/www/ProjectName. If DOCUMENT_ROOT is only /home/user/www then you can change your include path to this:

include $_SERVER['DOCUMENT_ROOT'] . '/ProjectName/helpers/ConfigOptions.php';

or use a relative path. My vote would be on the latter since you wouldn't need to hardcode "ProjectName".

So include() and require() would take either:

  1. An absolute path to the file
  2. A path that's relative to the current executing script. If A includes B and B includes C, when B includes C it needs to provide a path that is relative to A.

So when do you need to use DOCUMENT_ROOT? Usually with templates where a relative path like ../helpers/file.php can resolve to different absolute paths depending on what's including the file. In the above example where index.php is including ConfigOptions.php, you probably don't need to use DOCUMENT_ROOT since index.php is not a template. I used it anyway to be safe, but maybe I opened up a whole can of worms here. I hope I didn't confuse you even more.

ま昔日黯然 2024-12-17 18:39:38

如果您有一个以斜杠开头的链接,它总是会附加到根目录而不是当前目录...

因此,如果您的页面是www.example.com/mySite/foo。 html 并且您有一个如下链接: Bar/ 用户将被重定向到 www. example.com/bar.html ...

你应该只需将您的表单操作指向 helpers/login.php

If you have a link that starts with a slash it always gets appended to the root and not your current directory...

So if your page is www.example.com/mySite/foo.html and you have a link like this: <a href="/bar.html">Bar/<a> the user will get redirected to www.example.com/bar.html ...

You should just have your form action point to helpers/login.php

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