可能重复:
PHP中的"警告:已发送标头"
大家好我在PHP登录表单上显示错误,当我将其上传到我的服务器时.它在我的localhost上工作得很好,但是当我将它上传到服务器时,它给了我这个错误:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 45
Warning: Cannot modify header information - headers already sent by (output started at /home/shopping/public_html/biblioteka/index.php:10) in /home/shopping/public_html/biblioteka/index.php on line 49
Run Code Online (Sandbox Code Playgroud)
这是我的PHP代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="http://localhost/Shopping_biblioteka/css/style.css">
<title>Title</title>
</head>
<body align="center">
<div id="login">
<?php
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
$result = mysql_query("SELECT password,id FROM x9qg6_users
where username='" . $username . "'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
$test = explode(":", $row[0]);
$user_id=$row[1];
$userhash = md5($password . $test[1]);
if ($test[0] === $userhash) {
session_start();
$_SESSION['login_user'] = $user_id;
$_SESSION['username'] = $username;
$url = "biblioteka.php";
header("Location: $url");
}
} else {
echo '??????? ?? ?????? ???????? ?? ????????!';
}
// Connects to your Database
?>
<form action="" method="POST" accept-charset="UTF-8">
?????????? ???:<br/>
<input name="username" id="username" type="text"/><br/>
???????:<br/>
<input type="password" id="password" name="password"/><br/>
<input type="submit" value="??????? ??!"/>
</form>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
只需在session_start()之后添加ob_start ()
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
...
...
Run Code Online (Sandbox Code Playgroud)