可能重复:
PHP 已发送的标头
我正在尝试制作登录脚本。这是我到目前为止所拥有的,但是当我尝试使用有效用户登录时它会返回:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 6
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 6
Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/dir/login.php:3) in /home/user/public_html/dir/login.php on line 31
这是我的代码:
<?php
if(isset($_POST['login'])){
ob_start();
session_start();
$host="xxx";
$username_db="xxx";
$password_db="xxx";
$db_name="xxx";
$tbl_name="xxx";
mysql_connect("$host", "$username_db", "$password_db")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$email=$_POST['email'];
$password=$_POST['password'];
$email = stripslashes($email);
$password = stripslashes($password);
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$email' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
if($count==1){
$_SESSION['username'] = $email;
$_SESSION['password'] = $password;
header("location:main.php");
}
else {
echo"<br><center><div class=\"alert alert-error\">
Wrong email or password!</div></center>";
}
ob_end_flush();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
<meta name="description" content="">
<meta name="author" content="">
<link href="bootstrap/css/bootstrap.css" rel="stylesheet">
<style type="text/css">
html, body {
background-color: #eee;
}
body {
padding-top: 40px;
}
.container {
width: 300px;
}
.container > .content {
background-color: #fff;
padding: 20px;
margin: 0 -20px;
-webkit-border-radius: 10px 10px 10px 10px;
-moz-border-radius: 10px 10px 10px 10px;
border-radius: 10px 10px 10px 10px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
box-shadow: 0 1px 2px rgba(0,0,0,.15);
}
.login-form {
margin-left: 65px;
}
legend {
margin-right: -50px;
font-weight: bold;
color: #404040;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">hi</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="register.php">Register</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<div class="content">
<div class="row">
<div class="login-form">
<h2>Login</h2>
<form method="post" action="">
<fieldset>
<div class="clearfix">
<input name="email" type="text" placeholder="Email">
</div>
<div class="clearfix">
<input name="password" type="password" placeholder="Password">
</div>
<button name="login" class="btn btn-primary" type="submit">Sign in</button><br>
</fieldset>
</form>
</div>
</div>
</div>
</div> <!-- /container -->
</body>
</html>