Question: 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](https://i.stack.imgur.com/sIsYT.png)](https://i.stack.imgur.com/sIsYT.png)
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;
}

?>

```

Answer: 
Your redirect Syntax is not correct. Use like this:

```
header("Location: http://www.example.com/p1.php");

```
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');

```