Cannot modify header information - headers already sent by

ARy 2011-04-06 09:15:42
<?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 10

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 11
You must enter a username and password combination!

这个一个用户登录验证
查资料说header()要放在最前面,但是尽管可以吧第一次的header()放在最前面,但是我的第二个header()仍然会报错,求高手求救该怎么改?
...全文
138 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ImN1 2011-04-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hnxxwyq 的回复:]

PHP code


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

if (empty($_SESSION['user_id']))
{
/*第一个header()函数……
[/Quote]

原因是?require里面的问题?
lazyboy_wu 2011-04-06
  • 打赏
  • 举报
回复


<?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();
kyzy_yy_pm 2011-04-06
  • 打赏
  • 举报
回复
第二个header之前有输出

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

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧