简体   繁体   中英

Issue redirecting to the same page

I'm having trouble redirecting to the same page. I keep getting the message: "The localhost page isn't working, localhost redirected you too many times, ERR_TOO_MANY_REDIRECTS".

From my navbar there are drop downs for registering and logging in. Registering worked fine and so did the login for a few minutes then suddenly stopped working for some reason. I hadn't touched the code in over an hour.

 <?php session_start(); if(isset($_SESSION['usr_id'])) { header("Location: index.php"); } include_once 'dbconnect.php'; //set validation error flag as false $error = false; //check if form is submitted if (isset($_POST['signup'])) { $name = mysqli_real_escape_string($con, $_POST['name']); $email = mysqli_real_escape_string($con, $_POST['email']); $password = mysqli_real_escape_string($con, $_POST['password']); $cpassword = mysqli_real_escape_string($con, $_POST['cpassword']); //name can contain only alpha characters and space if (!preg_match("/^[a-zA-Z ]+$/",$name)) { $error = true; $name_error = "Name must contain only alphabets and space"; } if(!filter_var($email,FILTER_VALIDATE_EMAIL)) { $error = true; $email_error = "Please Enter Valid Email ID"; } if(strlen($password) < 6) { $error = true; $password_error = "Password must be minimum of 6 characters"; } if($password != $cpassword) { $error = true; $cpassword_error = "Password and Confirm Password doesn't match"; } if (!$error) { if(mysqli_query($con, "INSERT INTO users(name,email,password) VALUES('" . $name . "', '" . $email . "', '" . md5($password) . "')")) { $successmsg = "Successfully Registered! <a href='login.php'>Click here to Login</a>"; } else { $errormsg = "Error"; } } } if (isset($_POST['login'])) { $email = mysqli_real_escape_string($con, $_POST['email']); $password = mysqli_real_escape_string($con, $_POST['password']); $result = mysqli_query($con, "SELECT * FROM users WHERE email = '" . $email. "' and password = '" . md5($password) . "'"); if ($row = mysqli_fetch_array($result)) { $_SESSION['usr_id'] = $row['id']; $_SESSION['usr_name'] = $row['name']; header("Location: index.php"); die; } else { $errormsg = "Incorrect Email or Password!!!"; } } ?> 
  <div class="wraplogin"> <form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="loginform" id="edd_login_form" class="edd_form"> <fieldset> <legend>Log into Your Account</legend> <p> <label>Username</label> <input type="text" name="email" required class="form-control" id="edd_user_login" class="required edd-input" /> </p> <p> <label>Password</label> <input type="password" name="password" placeholder="Your Password" required class="form-control" id="edd_user_pass" class="password required edd-input" /> </p> <p> <input id="edd_login_submit" type="submit" class="edd_submit" name="login" value="Log In" /> </p> <li class="divider"></li> <p class="edd-lost-password"> <a href="#" title="Lost Password">Lost Password? </a> </p> </fieldset> </form> <span class="text-danger"><?php if (isset($errormsg)) { echo $errormsg; } ?></span> </div> 

Anyone know what the issue is?

Right at the beginning you do a redirect:

<?php
session_start();

if(isset($_SESSION['usr_id'])) {
    header("Location: index.php");
}

However after logged in ( $_SESSION['usr_id'] is set) if this page is index.php , it will redirect to itself, and check again and redirect to itself, again, entering a loop

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