Mustaghees Mustaghees - 1 year ago 86
PHP Question

Redirect loop issue in PHP website

I have been working on CS50's problem set 7, in which we have to make a financial website using MVC. I completed the website and it is working absolutely fine on my local machine.

But when I upload the files to hosting (free) service's server and try to access it I get a Redirect Loop error. Here is the link to it:

Here is code of



// configuration

// if user reached page via GET (as by clicking a link or via redirect)
// 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)

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



Here is login_form.php:

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

And this is config.php. This also has a redirect:


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

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

// requirements

// enable sessions

// 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"]))


I hope its easy to understand. I don't know where the problem lies and how to fix it.

I could have asked it on but a similar question is already there with no answer.

Answer Source

I did some debugging using bhushanRJ's advice of using die (). And found out that the issue is with URLs. So using /finance/public/login.php instead of just login.php (same for other array items) solved the issue.

However CSS and JS files weren't loading. Similarly, fixing their URLs in templates fixed the issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download