【问题标题】:HTML form action attribute landing on a file in another folder. Upon submitting error 404 file not foundHTML 表单动作属性登陆另一个文件夹中的文件。提交错误 404 文件未找到
【发布时间】:2021-05-06 23:26:36
【问题描述】:

我正在尝试实现一个简单的登录表单。 这是我的本地主机服务器文件夹结构的布局:

www/
    html/
        index.php
        home.php
        register.php
    controller/
        authenticate.php

我的索引 php 文件是一个简单的登录表单。见以下代码:

<?php
// enable session
session_start();

// redirect user to homepage if he is logged in (i.e. if his session is authenticated)
if (isset($_SESSION["authenticated"]) && $_SESSION["authenticated"]==true){
  $host = $_SERVER["HTTP_HOST"];
  $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
  header("Location: http://$host$path/home.php");
  exit;
}
?>


<html>
  <head>
    <title>Log In</title>
  </head>
  <body>
    <form action="../controller/authenticate.php" method="POST">
      <table>
        <tr>
          <td>Username:</td>
          <td>
            <input name="username" type="text"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input name="password" type="password"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" value="Log In"></td>
        </tr>
      </table>      
    </form>
    <a href="register.php"> <br> Register </a>
  </body>
</html>

我的 authenticate.php 文件只是一个 php 文档,用于检查数据库中是否有一行包含已发布的用户名和密码。如果用户名和密码正确,它会将用户重定向到 home.php。 当我尝试在 localhost/index.php 页面上提交登录表单时,我收到一个错误 404,说在服务器上找不到 URL http://localhost/controller/authenticate.php(但它肯定在那里)。

我知道问题出在表单操作属性上,但我不知道为什么它不起作用。路径似乎正确,浏览器中的 url 也显示了正确的路径。

关于表单操作属性登陆另一个文件夹中的脚本文件的主题有类似的帖子(请参阅下面的链接)。 我经历了它们,但仍然无法弄清楚我的问题。 请帮忙:)

另外,有没有办法在表单操作属性中使用服务器超全局而不是手动输入路径?

类似帖子:

link1

link2

link_to_a_very_similar_post

【问题讨论】:

  • 你确定你没有移动authenticate.php文件
  • @sabaykebremso 是的,我确定。有人发表评论说我应该将我的服务器根目录重新配置为 www 文件夹而不是 html。我试过了,它奏效了。当我回来时,他的评论消失了。谁写的,谢谢。
  • 好的,干得好:)

标签: php html forms attributes http-status-code-404


【解决方案1】:

有人发表评论说我应该将我的服务器根目录重新配置为 www 文件夹而不是 html。我试过了,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 2016-05-17
    • 2023-01-26
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多