dpq59734 2013-11-03 07:11
浏览 46
已采纳

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

I've looked through this about a hundred times and can't find a reason the page won't redirect. I can't find any spaces or any output I might be missing. Can anyone spot the problem?

Here is my code:

<?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 {
} 
?>

Here is the function for redirecting:

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

Here are the validation functions:

$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条回答 默认 最新

  • duanhan5230 2013-11-03 07:16
    关注

    Most likely you have any output (HTML or error messages) before calling the header-function. All headers should be sent before you can send the output.

    Some output you can't see, but is there. Such as spaces etc. Try removing the

    ?>
    <?php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题