<?php 
session_start();
require_once('db_login.php');
require_once('DB.php');

if (empty($_SESSION['user_id']))
{
                  /*第一个header()函数总是报错*/
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
{
header('WWW-Authenticate: Basic realm = "Member Area"');
header("HTTP/1.0 401 Unauthorized");
echo "You must enter a username and password combination!";
exit;
}
}

$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection))
{
echo "Could not connect to the data base:<br />".DB::errorMessage($connection);
}

$username = mysql_real_escape_string($_SERVER['PHP_AUTH_USER']);
$password = mysql_real_escape_string($_SERVER['PHP_AUTH_USER']);
$query = "select user_id, username from users where username = '".$username."' and  password = MD5('".$password."') limit 1";
$result = $connection->query($query);

if (!$row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{/*第二个header()*/
header('WWW-Authenticate: Basic realm = "Member Area"');
header("HTTP/1.0 401 Unauthorized");
echo "Your username and password combination was incorrect!";
exit;
}

$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];

echo "You have successfully logged in as: ".$_SESSION["username"].".";

?>Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\db_login.php:7) in C:\xampp\htdocs\user_login_main.php on line 10Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\db_login.php:7) in C:\xampp\htdocs\user_login_main.php on line 11
You must enter a username and password combination!这个一个用户登录验证
查资料说header()要放在最前面,但是尽管可以吧第一次的header()放在最前面,但是我的第二个header()仍然会报错,求高手求救该怎么改?

解决方案 »

  1.   

    第二个header之前有输出要注意:header之前不能有任何输出(包括空格标签等都属于输出)、错误
      

  2.   

    <?php 
        ob_statr();
        session_start();
        require_once('db_login.php');
        require_once('DB.php');
        
        if (empty($_SESSION['user_id']))
        {
                      /*第一个header()函数总是报错*/
            if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
            {
                header('WWW-Authenticate: Basic realm = "Member Area"');
                header("HTTP/1.0 401 Unauthorized");
                ob_clean_flush();
                echo "You must enter a username and password combination!";
                exit;
            }
        }    
        ob_clean_flush();
      

  3.   


    原因是?require里面的问题?