登录后为什么我的页面没有重定向?

时间:2013-11-03 07:11:13

标签: php redirect header

我已经查看了大约一百次,找不到页面不会重定向的原因。我找不到任何空格或任何我可能遗失的输出。有谁能发现问题?

这是我的代码:

<?php
include_once('includes/sessions.php');
include_once('connections/localhost.php');
include_once('includes/functions.php');
include_once('includes/validations.php');
?>
<?php
$username = "";
if (isset($_POST['submit'])) {
  $required_fields = array("username", "password");
  validate_there($required_fields);
  if (empty($errors)) {
        $username = $_POST["username"];
        $password = $_POST["password"];
        $found_user = attempt_login($username, $password);
    if ($found_user) {
    $_SESSION["user_id"] = $found_user["user_id"];
    $_SESSION["username"] = $found_user["username"];
    redirect_to("index.php");     
    } else {
      $_SESSION["message"] = "Username/password not found.";
    }
  }
} else {
} 
?>

以下是重定向功能:

function redirect_to($new_location) {
      header("Location: " . $new_location);
      exit;
    }

以下是验证功能:

$errors = array();

function fieldname_is_text($fieldname) {
  $fieldname = str_replace("_", " ", $fieldname);
  $fieldname = ucfirst($fieldname);
  return $fieldname;
}


function has_presence($value) {
    return isset($value) && $value !== "";
}

function validate_there($required_fields) {
  global $errors;
  foreach($required_fields as $field) {
    $value = trim($_POST[$field]);
    if (!has_presence($value)) {
        $errors[$field] = fieldname_is_text($field) . " can't be blank";
    }
  }
}

2 个答案:

答案 0 :(得分:1)

在调用header-function之前,很可能有任何输出(HTML或错误消息)。在发送输出之前,应先发送所有标题。

有些输出你看不到,但是有。如空格等。尝试删除

?>
<?php

答案 1 :(得分:0)

尝试使用此功能进行重定向。

<?php
function redirect_to($new_location)
{
    ?>
<script type="text/javascript">
    window.location = '<?php echo $new_location;?>';
</script>
<?php
}

?>