The-coder
The-coder

Reputation: 61

Why is my local host unable to rediect to other pages?

I have a following php code:I am at signin.php

<?php
 session_start();
 if(!isset($_POST['firstName']) && !isset($_POST['lastName']) && !isset($_POST['email']) && 
 !isset($_POST['password']) && !isset($_POST['confirmedPassword'])){
 $_SESSION['error']="Please Fill Out all the fields";
 header("Location:signin.php");
    return;
}
else{
if($_POST['password']!=$_POST['confirmedPassword']){
    $_SESSION['error']="Passwords don't match.Retry!!";
    header("Location:signin.php");
    return;
    }else{
       header("Location:index.php");
    return;
    }
  }
 ?>

In my html form I created a sign Up form which enables the users to sign in themselves. However when I run the code my localhost says

localhost redirected you too many times.
 ERR_TOO_MANY_REDIRECTS

Upvotes: 1

Views: 45

Answers (2)

Hackinet
Hackinet

Reputation: 3338

You are redirecting the user to the sign-in page if any of those five $_POST[] is empty. When you do a GET request $_POST[] would naturally be empty and hence you are stuck in an infinite loop. So before checking if those $_POST[] variables exist, you just check the request type too.

<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') //<--check request type
{
    if (empty($_POST['firstName']) || empty($_POST['lastName']) ||  empty($_POST['email']) || empty($_POST['password']) || empty($_POST['confirmedPassword']))
    {
        $_SESSION['error'] = "Please Fill Out all the fields";
        header("Location:signin.php");
    }
    else
    {
        if ($_POST['password'] != $_POST['confirmedPassword'])
        {
            $_SESSION['error'] = "Passwords don't match.Retry!!";
            header("Location:signin.php");
        }
        else
        {
            header("Location:index.php");
        }
    }
}
exit;
?>

In addition:

  • Replaced empty() in place of !isset(). It's better.
  • It's should be || and not &&. You need to check if any of them are empty.
  • Replaced return with exit. And moved the exit to the last line.

Upvotes: 1

Ghlen
Ghlen

Reputation: 659

You are redirecting to the sign in page. You will probably be stuck in an infinite loop where the post variables are never filled in (it's a get request). Thus redirecting too many times, where the browser will pick up on and stop the infinite loop.

Upvotes: 1

Related Questions