PHP中的页面重定向(标题)问题

时间:2017-02-20 08:09:15

标签: php redirect header

我需要您对PHP代码的帮助。我正在制作简单的登录表单,但是当我成功登录时收到错误。我想在登录成功后重定向到另一个网页。

浏览器错误:(请参阅附图)enter image description here localhost页面无效 localhost重定向了你太多次了。 尝试清除您的cookie。 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;
}

?>

2 个答案:

答案 0 :(得分:1)

您的重定向语法不正确。使用方式如下:

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

答案 1 :(得分:0)

像这样使用:

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