我已经面对这个问题两天,我已经从多个帖子中学习。这些来自How to fix "Headers already sent" error in PHP和"Warning: Cannot modify header information - headers already sent by" error,但我仍然无法解决问题。
我的代码如下:
<?php
ob_start();
session_start();
require_once('../dbconnection.php');
session_name(getSessionName());
if ((!empty($_SESSION["login"]))){
header ("Location: summary.php");
}
session_regenerate_id(true);
$module = 'acc';
?>
<!DOCTYPE html>
<html>
<head>
<?php
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include('../templates/_common.php');
?>
<title>ACC - Login</title>
</head>
<style>
#theForm label.error {
position:relative;
color:red;
font-family:arial;
font-weight:bold;
font-size:15px;
}
</style>
<body>
<?php
include('../templates/_loginheader.php');
?>
<div class="container">
<!--Body-->
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-md-12">
<div class="logo-div"><img src="../images/logo.jpg"></div>
<br/>
<!-- Login -->
<?php
include_once "password.php";
$uname = "";
$pword = "";
if ($_POST) {
$uname = $_POST["username"];
$pword = $_POST["password"];
$uname = htmlspecialchars($uname);
$pword = htmlspecialchars($pword);
$link = connectDB();
$db_found = mysql_select_db(getDBName(), $link);
if ($db_found) {
$sql = sprintf("SELECT * FROM user u, employee e WHERE u.userId = e.userId AND username = '%s' AND u.status !='DELETE'",mysql_real_escape_string($uname));
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if ($result) {
if ($num_rows > 0 && password_verify($pword,$row['password'])) {
$_SESSION["login"] = "1";
$_SESSION["name"] = $_POST["username"];
$_SESSION["userid"] = $row["userId"];
$_SESSION["first"] = $row["firstName"];
$_SESSION["fullname"] = $row["firstName"]." ".$row["lastName"];
$_SESSION["emp"] = $row["firstName"];
$_SESSION["empid"] = $row["employeeId"];
$_SESSION["branchId"] = $row["locationId"];
$_SESSION["role"] = $row["role"];
if ($row["super"] == 1)
$_SESSION["super"] = 1;
if ($row["isAdmin"] == 1)
$_SESSION["sup"] = 1;
//clock attendance if logged in
clockAttendance();
header ("Location: summary.php");
}
else
echo("<div align=center class=\"alert alert-danger\"><b>Incorrect username or password</b></div>");
}
mysql_close($link);
}
else
echo("<div align=center class=\"alert alert-danger\">Database error</div>");
}
?>
<form class="form-horizontal" role="form" id="theForm" action="index.php" method=post>
<div class="form-group">
<label for="username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Sign in</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="../js/jquery-1.10.2.js"></script>
<script src="../js/jquery.validate.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$("#theForm").validate({
rules: {
username: {
required: true,
},
password: {
required: true,
}
},
messages: {
username: {
required: "Please enter a username",
},
password: {
required: "Please provide a password",
},
}
});
});
</script>
</body>
</html>
所以每当我从页面登录时,这些都是显示的错误。
任何人都可以建议并教我如何实现它吗?提前致谢。 注意:我对PHP很新,所以如果有什么不对的地方,请随时指导正确的路径。谢谢。