简体   繁体   中英

localhost redirected you too many times. ERR_TOO_MANY_REDIRECTS

I keep getting this error since last night for my index.php page only. i have cleared all my cookies but still i cant access index.php on local host. i have looked at other solutions but couldn't find an answer.

I think it is to do with my index.php code which redirects me to the same page to login. but i am new to web development i am not sure how to tackle the issue. below i have provided snippet of my index.php and login.php. any help is highly appreciated.

login.php

 <?php session_start(); // Change this to your connection info. $DB_HOST = 'localhost'; $DB_USER = 'root'; $DB_PASS = ''; $DB_NAME = 'ecom'; // Try and connect using the info above. $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if ( mysqli_connect_errno() ) { // If there is an error with the connection, stop the script and display the error. die ('Failed to connect to MySQL: ' . mysqli_connect_error()); } // Now we check if the data from the login form was submitted, isset() will check if the data exists. if ( !isset($_POST['username'], $_POST['password']) ) { // Could not get the data that should have been sent. die ('Please fill both the username and password field!'); } if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) { // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s" $stmt->bind_param('s', $_POST['username']); $stmt->execute(); // Store the result so we can check if the account exists in the database. $stmt->store_result(); } if ($stmt->num_rows > 0) { $stmt->bind_result($id, $password); $stmt->fetch(); // Account exists, now we verify the password. // password_hash function in is used in registration file to store the hashed passwords. if (password_verify($_POST['password'], $password)) { // Verification success! User has loggedin! // Create sessions so we know the user is logged in, they basically act like cookies but remember the data on the server. session_regenerate_id(); $_SESSION['loggedin'] = TRUE; $_SESSION['name'] = $_POST['username']; $_SESSION['id'] = $id; /*echo 'Welcome ' . $_SESSION['name'] . '!';*/ header('Location: index.php'); } else { echo 'Incorrect password!'; } } else { echo 'Incorrect username!'; } $stmt->close(); ?> 

index.php

 <?php // We need to use sessions, so you should always start sessions using the below code. session_start(); // If the user is not logged in redirect to the login page... if (!isset($_SESSION['loggedin'])) { header('Location:index.php'); exit(); } ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <title>City Outlet</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"> <link href="css/style.css?10" rel="stylesheet" type="text/css"> <!-- awesome-font CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head> <body> <!--========== Top bar Area ============--> <header class="topbar"> <nav> &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <a style="color:white; font-size:18" href="logout.php"><i class="fa fa-sign-out style="font-size:24px; color:white;""></i>Logout</a> &nbsp; &nbsp; <button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#loginModal" ><i class="fa fa-sign-in" style="font-size:24px" value="login" onClick="showModal()" ></i> Login</button> &nbsp; &nbsp; <button href="" class="btn login btn-default d-inline-flex" data-toggle="modal" data-target="#signupModal"><i class="fa fa-user" style="font-size:24px" ></i> Sign Up</button> &nbsp; &nbsp; &nbsp; </nav> <p style="color:white; font-weight:bold; text-align:right; line-height:-10px; margin-top: 7px;"> Welcome: <strong style="color:red; font-weight:bold;"><?=$_SESSION['name']?></strong>! &nbsp;<img src="images/ava.png" style=width:25px; height="25px;"> </p> </header> <!-- Login Modal --> <div class="loginbox" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <img class="avatar" src="images/redavatar.png"> <h1>Sign in</h1> <form action="login.php" method="post"> <p>Username</p> <input type="text" name="username" placeholder="Enter username" required> <p>Password</p> <input type="password" name="password" placeholder="Enter password" required> <input type="submit" name="submit" value="submit"> &nbsp; &nbsp; <a href="signup.php">Not a member?</a> <br> <br> </form> </div> <!----- Sign up Modal -----> <div class="container"> <h1 class="text-white bg-dark text-center"></h1> <div class="modal" id="signupModal"> <div class="modal-dialog "> <div class= "modal-content"> <div class="modal-header"> <h3 >Sign Up</h3> <button style="color:red" type="button" class="close" data-dismiss="modal" > &times;</button> </div> <div class="modal-body"> <form action="signup.php" method="post" > <div class="form-group"> <label><i class="fa fa-user fa-2x"></i> First name: </label> <input type="text" name="username" class="form-control" placeholder="your name" required> </div> <div class="form-group"> <label><i class="fa fa-lock fa-2x"></i> Password: </label> <input type="password" name="password" class="form-control" placeholder="choose password" required> </div> <div class="form-group"> <label><i class="fa fa-envelope fa-2x"></i> Email: </label> <input type="text" name="email" class="form-control" placeholder="Your email" required> </div> <input type="submit"> </form> </div> </div> </div> </div> </div> <!----- Sign up Modal -----> <!--========== End Top bar Area ============--> <nav class="navbar fixed-top navbar-expand-md navbar-light "> <div class="container-fluid"> <a class="navbar-brand " href="index.php"> <img src="images/citylogo.png" alt=""> </a> <button class="navbar-toggler third-button darken-3" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <div class="animated-icon3"><span></span><span></span><span></span></div> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="index.php">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item"> <a class="nav-link" href="#">Services</a> </li> <li class="nav-item"> <a class="nav-link" href="shop.php">Shop</a> </li> <li class="nav-item"> <a class="nav-link" href="booking.php">Booking</a> </li> <li class="nav-item"> <a class="nav-link" href="gallery.html">Gallery</a> </li> <li class="nav-item"> <a class="nav-link" href="contact.html">Contact</a> </li> </ul> <form class="searchbar" action="/action_page.php" style="margin:auto;max-width:300px"> <button type="submit"><i class="fa fa-search"></i></button> <input type="text" placeholder="Search.." name="search2"> </form> </div> </div> </nav> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script> <script> function showModal() { document.getElementById("loginModal").style.display = 'block'; } </script> <script> // Get the modal var modal = document.getElementById('id01'); // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script> </body> </html> 

On index.php This part of the code.

if (!isset($_SESSION['loggedin'])) {
    header('Location:index.php'); // redirecting to same page.
    exit();
}

You are redirecting to the same page if the user is not log in, thats why you have the infinite redirects... You must redirect to other page like a login page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM