【问题标题】:Redirect loop issue in PHP websitePHP网站中的重定向循环问题
【发布时间】:2015-09-28 16:51:13
【问题描述】:

我一直在研究 CS50 的第 7 题,其中我们必须使用 MVC 制作一个金融网站。我完成了网站,它在我的本地机器上运行良好。

但是当我将文件上传到托管(免费)服务的服务器并尝试访问它时,我收到重定向循环错误。这是它的链接:http://ghazilajpal.byethost6.com/finance/public/

这里是login.php的代码:

<?php

    // configuration
    require("../includes/config.php"); 

    // if user reached page via GET (as by clicking a link or via redirect)
    if ($_SERVER["REQUEST_METHOD"] == "GET")
    {
        // render form
        render("login_form.php", ["title" => "Log In"]);
    }

    // else if user reached page via POST (as by submitting a form via POST)
    else if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // validate submission
        if (empty($_POST["username"]))
        {
            apologize("You must provide your username.");
        }
        else if (empty($_POST["password"]))
        {
            apologize("You must provide your password.");
        }

        // query database for user
        $rows = query("SELECT * FROM users WHERE username = ?", $_POST["username"]);

        // if we found user, check password
        if (count($rows) == 1)
        {
            // first (and only) row
            $row = $rows[0];

            // compare hash of user's input against hash that's in database
            if (crypt($_POST["password"], $row["hash"]) == $row["hash"])
            {
                // remember that user's now logged in by storing user's ID in session
                $_SESSION["id"] = $row["id"];
                $_SESSION["cash"] = $row["cash"];
                // redirect to index.php (portfolio)
                redirect("/");
            }
        }

        // else apologize
        apologize("Invalid username and/or password.");
    }

?>

更新

这里是 login_form.php:

<form action="login.php" method="post">
    <fieldset>
        <div class="form-group">
            <input autofocus class="form-control" name="username" placeholder="Username" type="text"/>
        </div>
        <div class="form-group">
            <input class="form-control" name="password" placeholder="Password" type="password"/>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-default">Log In</button>
        </div>
    </fieldset>
</form>
<div>
    or <a href="register.php">register</a> for an account
</div>

这是 config.php。这也有一个重定向:

<?php

    /**
     * config.php
     *
     * Computer Science 50
     * Problem Set 7
     *
     * Configures pages.
     */

    // display errors, warnings, and notices
    ini_set("display_errors", true);
    error_reporting(E_ALL);

    // requirements
    require("constants.php");
    require("functions.php");

    // enable sessions
    session_start();

    // require authentication for all pages except /login.php, /logout.php, and /register.php
    if (!in_array($_SERVER["PHP_SELF"], ["/login.php", "/logout.php", "/register.php"]))
    {
        if (empty($_SESSION["id"]))
        {
            redirect("login.php");
        }
    }

?>

我希望它易于理解。我不知道问题出在哪里以及如何解决。

我本可以在 cs50.stackexchange.com 上问它,但已经有一个类似的问题没有答案。

【问题讨论】:

  • 这不足以帮助您。您的重定向循环在 login.php 中,使用 GET 方法,这就是我从您的代码中可以看出的全部内容。也许,尝试发布 include/config.php 中的内容(从那里删除任何私人数据)以及 login_form.php
  • @Palantir 我按照你的要求编辑了问题

标签: php redirect-loop


【解决方案1】:

它总是出现在这个代码块中吗?

if (crypt($_POST["password"], $row["hash"]) == $row["hash"])
            {
                // remember that user's now logged in by storing user's ID in session
                $_SESSION["id"] = $row["id"];
                $_SESSION["cash"] = $row["cash"];
                // redirect to index.php (portfolio)
                redirect("/");
            }

如果是,那么这里有一个问题。 用 die() 改变重定向;验证。

请提供更多输入以澄清更多信息。

【讨论】:

  • 我添加了更多代码。并尝试了您的解决方案,但仍然重定向循环
  • 感谢您的建议帮了大忙。
【解决方案2】:

我使用 bhushanRJ 的使用 die () 的建议进行了一些调试。并发现问题出在 URL 上。所以使用/finance/public/login.php 而不是 login.php(其他数组项相同)解决了这个问题。

但是 CSS 和 JS 文件没有加载。同样,在模板中修复它们的 URL 也解决了这个问题。

【讨论】:

    【解决方案3】:

    当我得到一个循环时,那是因为我没有通过输入“session_start()”来启动会话

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 2014-03-24
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多