PHP重定向不工作标头已发送

时间:2014-04-21 17:32:33

标签: php

所以我这里有一些代码只使用电子邮件地址进行基于MySQL的登录。这部分代码非常有用。我有的是当我在验证电子邮件地址后尝试重定向到请求的页面时。这是用于WiFi身份验证,因此标头功能正在发送到之前连接的动态字符串变量。但是,当调用头函数时,它只会将我转储回主目标网页。

对其进行错误处理会报告臭名昭着的“已发送的标头”。但是,我已经仔细检查了我的代码,并在书挡之前和之后清理了回声,打印和空白。我真的需要一些帮助,因为我无法找到问题和ob_start(); ob_end_flush()函数;是不是真的工作...提前谢谢!!!

logon.php

<?php
ob_start();
//Uncomment this section for troubleshooting
/*
  error_reporting(E_ALL | E_WARNING | E_NOTICE);
  ini_set('display_errors', 1);
  ini_set('display_startup_errors', 1);
*/
  //Connect to the database
  include_once 'includes/db_connect.php';

  //Connect the functions
  include_once 'includes/functions.php';

  //Start Secure Session
  sec_session_start();

  //Define Some Variables
  $email = "";
  $base_grant_url = urldecode($_GET['base_grant_url']); //get info from meraki redirect
  $user_continue_url = urldecode($_GET['user_continue_url']); //get info from meraki redirect
  $url = $base_grant_url.$cont.$user_continue_url.$dur;

  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = htmlspecialchars(($_POST["email"]));
    $email = test_input($email);
    if(!isset($errorMsg)) {
      if (login($email, $dbh)) {
        header("Location: $url");
        exit;
      }
      else //email not found in db
        {$errorMsg = "E-Mail address not found in database. Please retry and make sure you are using your Rent Cafe username.";}
    }
  }
ob_flush();
?>

的functions.php

<?php
ob_start();
define("SECURE", TRUE);
function sec_session_start() {

    $session_name = 'sec_session_id';   // Set a custom session name
    $secure = SECURE;
    // This stops JavaScript being able to access the session id.
    $httponly = true;
    // Forces sessions to only use cookies.
    if (ini_set('session.use_only_cookies', 1) === FALSE) {
        header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
        exit();
    }
    // Gets current cookies params.
    $cookieParams = session_get_cookie_params();
    session_set_cookie_params($cookieParams["lifetime"],
        $cookieParams["path"],
        $cookieParams["domain"],
        $secure,
        $httponly);
    // Sets the session name to the one set above.
    session_name($session_name);
    session_start();            // Start the PHP session
    session_regenerate_id();    // regenerated the session, delete the old one.
}

function login($email, $dbh) {

  $stmt = $dbh->prepare("SELECT email FROM tenants WHERE email = :email");
  $stmt->bindParam(':email', $email);
  $stmt->execute();

  if ($stmt->rowCount() > 0)
    {return true;}
  else {
    date_default_timezone_set('America/Los_Angeles');
    $current_date = date('m/d/Y == H:i:s');
    $time = time();
    $stmt2 = $dbh->prepare("INSERT INTO login_attempts (time, email_id) VALUES (?, ?)");
    $stmt2->bindParam(1, $current_date);
    $stmt2->bindParam(2, $email);
    $stmt2->execute();
    return false;
  }
}

function test_input($data) {

  $errorMsg = "";

  if (empty($data)) {      //left form blank
    $errorMsg = "Email is required";
    return $data;
  }
  else {
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     // check if e-mail address syntax is valid
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$data))
       {$errorMsg = "Invalid email format";}
     return $data;
  }
}
?>

db_connect.php

<?php
ob_start();
$dbhost = '****';
$dbuser = '****';
$dbpass = '****';
$db = '****';
$cont = '?continue_url=';
$dur = '&duration=1440';

try {

  $dbh = new PDO("mysql:host=$dbhost;dbname=$db", $dbuser, $dbpass);
}

catch (PDOException $e) {

  die();

}
?>

2 个答案:

答案 0 :(得分:1)

 <?php
^------ single space, which is "output"
ob_start();

答案 1 :(得分:0)

对于编写PHP代码,需要<?php,结束标记?>是可选的,

如果我们将?>放在文件末尾,您的网络服务器可能会在其后面添加一些空格,

这就是为什么没有重定向的原因。

只需删除最后的?>

注意:这是Drupal和CodeIgniter遵循的标准做法。