21,887
社区成员
发帖
与我相关
我的任务
分享
<?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"].".";
?>
<?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();