php 警告:session_start() [function.session-start]:无法发送会话 cookie - 头已经发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13742038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by
提问by Chris
Possible Duplicate:
“Warning: Headers already sent” in PHP
可能的重复:
PHP 中的“警告:标题已经发送”
Hi guys I have a error that displays on my PHP login form, when I upload it to my server. It was working perfectly on my localhost, but when I upload it to server it gives me this error:
大家好,当我将 PHP 登录表单上传到我的服务器时,它显示在我的 PHP 登录表单上。它在我的本地主机上运行良好,但是当我将它上传到服务器时,它给了我这个错误:
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
And here is my PHP code:
这是我的 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>
回答by Pintu Francis
Just add ob_start()after session_start()
只需在session_start()之后添加ob_start ()
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
...
...
回答by Sherif
Yes, your issue is that you are trying to send headers (by calling *session_start*) after you have already sent some output. This could be anything from HTML/whitespace/error-messages/etc.. that occurred before you called on *session_start*.
是的,您的问题是您在已经发送了一些输出之后尝试发送标头(通过调用 *session_start*)。这可能是在您调用 *session_start* 之前发生的 HTML/whitespace/error-messages/etc.. 中的任何内容。
In the HTTP protocol those headers must come before the body of the request/response and since you've inadvertently started to send output you are already in the body and can't go back to the header.
在 HTTP 协议中,这些标头必须位于请求/响应正文之前,并且由于您无意中开始发送输出,因此您已经在正文中并且无法返回标头。
One solution is to always make sure you call *session_start* before you do anything else. Another option is to use output bufferingfunctions like ob_start()to buffer any output just in case.
一种解决方案是始终确保在执行任何其他操作之前调用 *session_start*。另一种选择是使用像ob_start()这样的输出缓冲函数来缓冲任何输出以防万一。
Just in case you are wondering what's really happening that causes this here's a brief explanation of what session_start normally does... The normal behavior for a PHP session is to do 3 things when you call the function session_satrt()
以防万一您想知道到底是什么导致了这种情况,这里简要说明 session_start 通常会做什么...... PHP 会话的正常行为是在调用函数session_satrt()时做 3 件事
- The session handler checks if the client has supplied a session id(normally by sending a cookie in the HTTP request header with the specified session.name). If one is found the session handler opens that session (by default that's a file -- if it doesn't exist it's created). If no session idis supplied by the client a random session id is generated and that session file is opened.
- The session handler than populates any data found in the session file into the $_SESSIONsupperglobal variable (i.e. the session is de-serialized).
- If in step 1 no Session ID was supplied (the session cookie) then the session handler sets a Set-Cookieheader to be sent with the HTTP response.
- 会话处理程序检查客户端是否提供了会话 ID(通常通过在具有指定session.name的 HTTP 请求标头中发送 cookie )。如果找到,会话处理程序将打开该会话(默认情况下,这是一个文件——如果它不存在,则创建它)。如果没有会话ID是由客户端提供的生成随机会话ID和会话文件被打开。
- 会话处理程序然后将会话文件中找到的任何数据填充到$_SESSION超全局变量中(即会话被反序列化)。
- 如果在步骤 1 中没有提供会话 ID(会话 cookie),则会话处理程序设置一个Set-Cookie标头以与 HTTP 响应一起发送。
回答by NullPoiиteя
you must not output anything to the page before sent the header
在发送标头之前,您不得向页面输出任何内容
or use output buffering
或使用输出缓冲
ob_start();
回答by Bart Friederichs
Make sure you do not send anything(not even whitespace) before the session_start()or header()call:
确保在or调用之前没有发送任何内容(甚至没有空格):session_start()header()
<?php
...
session_start();
...
header();
...
?>
<!-- HTML code -->
More info can be found on the session_startdocumentation page.
更多信息可以在session_start文档页面上找到。
session_startwill send information in the HTTP headers (a piece of information sent to the browser, before any real content is sent). If you have already sent content to the browser, e.g. in your case quite some HTML code, the server cannot send that information in the headers: they are already gone. Read more on the HTTP protocolto understand the whole thing better.
session_start将在 HTTP 标头中发送信息(在发送任何实际内容之前发送到浏览器的一条信息)。如果您已经将内容发送到浏览器,例如在您的情况下相当多的 HTML 代码,则服务器无法在标题中发送该信息:它们已经消失了。阅读有关HTTP 协议的更多信息以更好地理解整个事情。
回答by Muthu Kumaran
Move session_start();to top of the file.
移至session_start();文件顶部。
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
...
...

