问题重定向到同一页面

时间:2016-04-08 22:13:53

标签: php loops redirect login

我无法重定向到同一页面。我不断收到消息:“localhost页面无效,localhost重定向了你太多次,ERR_TOO_MANY_REDIRECTS”。

在我的导航栏中有注册和登录的下拉列表。注册工作正常,登录几分钟,然后由于某种原因突然停止工作。我在一个多小时内没有触及过代码。

<?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>

任何人都知道这是什么问题?

1 个答案:

答案 0 :(得分:0)

刚开始时你会进行重定向:

class Bar {
  def bar(): Unit = {
    val foo = new Foo()
    foo.foo(flag = true)
  }
}

但是,如果此页面为 index.php ,则在登录(<?php session_start(); if(isset($_SESSION['usr_id'])) { header("Location: index.php"); } 后)时,它将重定向到自身,然后再次检查并重定向到自身,再次进入循环