1

I need your help with regards to my code in PHP. I am making simple login form but I am getting an error when I login successfully. I want to redirect to another webpage when i login successfully.

Browser error: (Please see attached image)enter image description here The localhost page isn’t working localhost redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS

<?php

$errors = array();
if(isset($_POST['submit'])) {
    $username = trim($_POST["username"]);
    $password = trim($_POST["password"]);

    if(!isset($username) || empty($username) || !isset($password) || empty($password) ) {
        $errors['$blank'] = "Fields can't be blank.";
    }

    else {
        $errors = "";
    }

    $min = 6;
    if(strlen($username) < $min || strlen($password) < $min) {
        $errors['$minAllowed'] = "Only minimum of 6 characters is allowed";
    }

    $max = 12;
    if(strlen($username) > $max || strlen($password) > $max) {
        $errors['$minAllowed'] = "Only maximum of 12 characters is allowed";
    }

}

function form_errors($errors=array()) {
    $output = "";
    if(!empty($errors)) {
        $output = "<div class=\"error\">";
        $output .= "Please fix the following errors:";
        $output .= "<ul>";
            foreach ($errors as $key => $error) {
                $output .= "<li>{$error}</li>";
            }
        $output .= "</ul>"; 
        $output .= "</div>";    
    }
    else {
         header("Location: " . "p1.php");
    }


    return $output;
}

?>
Nawin
  • 1,653
  • 2
  • 14
  • 23

2 Answers2

1

Your redirect Syntax is not correct. Use like this:

header("Location: http://www.example.com/p1.php");
Nawin
  • 1,653
  • 2
  • 14
  • 23
0

Use like this:

  function redirect_to($location)
  {
      if (!headers_sent()) {
          header('Location: ' . $location);
          exit;
      } else {
          echo '<script type="text/javascript">';
          echo 'window.location.href="' . $location . '";';
          echo '</script>';
          echo '<noscript>';
          echo '<meta http-equiv="refresh" content="0;url=' . $location . '" />';
          echo '</noscript>';
  }}


redirect_to('http://www.example.com/p1.php');