Leahcim
Leahcim

Reputation: 41929

php redirect loop

I'm following a php video tutorial (it's not online sorry) from Lynda.com and used the following code, but I got the following error

Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

could this be a problem with my code. i.e. the fact that the code has two redirect_to in the first 10 or 15 lines, or is it talking about something else?

<?php require_once("../../includes/initialize.php"); ?>

<? if(!$session->is_logged_in()){
    redirect_to("login.php"); } ?>

<?php
$logfile = SITE_ROOT.DS.'logs'.DS.'log.txt';
if($_GET['clear'] == 'true') {
 file_put_contents($logfile, '');
 //add the first log entry
 log_action('Logs Cleared', "by User ID {$session->user_id}");
 //redirect to this same page so that the URL won't 
 //have "clear=true" anymore
 redirect_to('logfile.php');
}
?>

<?php include_layout_templates('admin_header.php');?>

<a href="index.php">&laquo; Back</a><br/>

<br/>

<h2>Log File</h2>

<p><a href="logfile.php?clear=true">Clear log file</a></p>

<?php
if (file_exists($logfile) && is_readable($logfile) && 
    $handle = fopen($logfile, 'r')) {//read
    echo "<ul class=\"logentries\">";
    while(!feof($handle)) {
    $entry = fgets($handle);
    if(trim($entry) != "") {
    echo "<li>{$entry}</li>";
    }
    }
    echo "</ul>";
    fclose($handle);
    } else {
    echo "Could not read from {$logfile}.";
    }

?>



//Remember to give your form's submit tag a name="submit" attribute
if (isset($_POST['submit'])) {//Form has been submitted.

$username = trim($_POST['username']);
$password = trim($_POST['password']);

//Check database to see if username/password exist

$found_user = User::authenticate($username, $password);

if ($found_user) {
    $session->login($found_user);
    log_action('Login', "{$found_user->username} loggined in.");
    redirect_to("index.php");
} else {
    //username/password combo was not found in the database
    $message = "Username/password combination incorrect.";
} 
} else {//Form has not been submitted.
    $username = "";
    $password = "";
    }
?>
<?php include_layout_template('admin_header.php'); ?>
        <h2>Staff Login</h2>
        <?php echo output_message($message); ?>

        <form action="login.php" method="post">
            <table>
                <tr>
                    <td>Username:</td>
                    <td>
                        <input type="text" name="username" maxlength="30" value="<?php
                        echo htmlentities($username); ?>" />
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td>
                        <input type="password" name="password" maxlength="30" value="<?php
                        echo htmlentities($password); ?>" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" name="submit" value="login" />
                    </td>
                </tr>
            </table>
        </form>
        <?php include_layout_template('admin_footer.php'); ?>

Upvotes: 0

Views: 11920

Answers (3)

Jeff Parker
Jeff Parker

Reputation: 7507

<? if(!$session->is_logged_in()){
    redirect_to("login.php"); } ?>

Therein lies your problem I think. You're checking on your login page, to see if someone is logged in or not. If they're not, you'll redirect to your login page, starting a new request, and it'll perform the check again.

  • Login page asks, is the user logged in? No! Redirect them to the login page
  • Login page asks, Is the user logged in? No! Redirect them to the login page
  • Login page asks, Is the user logged in? No! Redirect them to the login page
  • ad-infinitum

People shouldn't have to be logged in to use the login page, so remove the check to see if someone's logged in before they use said page.

Upvotes: 2

Svish
Svish

Reputation: 158051

  • Check if your login page redirects if you're not logged in.
  • Make sure there is no output before you redirect
  • Make sure you exit after you have done the redirect. In your code example you will end up with some whitespace before you call the redirect function as a result of that empty line between your require and if check. If I was you, I wouldn't jump in and out of php as much as you do when there is no need to. All the way down to your first link, I see only php, but yet you have 3 <?php and one <? (which is also a bad idea. I'd stick with using only <?php).

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You have an endless loop of redirecting.

"login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. "login.php" redirects to "login.php" if you're not logged in. etc.

You should probably make the redirect happen only when the current page is not "login.php"; i.e. remove that logic from this page.

Upvotes: 5

Related Questions