php session_start 错误是因为同一 session_start 操作“已发送标头”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28366881/
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
session_start errors because 'headers already sent' by the same session_start action?
提问by Francesca
I am trying to start a session in PHP in order to store data about a user ID (which will be used in mySQL DB).
我正在尝试在 PHP 中启动会话以存储有关用户 ID(将在 mySQL DB 中使用)的数据。
However when I start the session I get the following errors:
但是,当我开始会话时,出现以下错误:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/blah) in /Applications/MAMP/blah on line 38
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/blah) in /Applications/MAMP/blah on line 38
I understand that the error is saying it cannot start the session as the page is already submitted, or "header already sent" however I am very confused because the line it is referencing, line 38, IS the line that starts the session:
我知道错误是说它无法启动会话,因为页面已经提交,或者“标题已经发送”但是我很困惑,因为它引用的行,第 38 行,是启动会话的行:
Line 38: session_start();
So how can it say the headers are already sent by this line and that's the error?
那么怎么能说标题已经由这条线发送了,那就是错误呢?
Here is a shortened down section of my code. To note, there is portion of the HTML that is loaded in via AJAX using jQuery, could that be it?
这是我的代码的缩短部分。请注意,有一部分 HTML 是使用 jQuery 通过 AJAX 加载的,是这样吗?
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<div class="wrapper">
<div id="quiz-section">
// This part is where the HTML content loads via AJAX
</div>
</div>
<!-- Below PHP looks at the referral i.e how the user landed on this page -->
<?php
session_start();
require 'connect.php';
if (!mysqli_query($con,"INSERT INTO entries (referral) VALUES ('$ref')")){
echo("Error description: " . mysqli_error($con));
return false;
}
$_SESSION["sessionID"] = mysqli_insert_id($con);
mysqli_close($con);
?>
</body>
<script>
// some jQuery here to load in the HTML content to the AJAX pane
</script>
</html>
回答by
You're outputting HTML before the session_start()
. Put your PHP code above the HTML code.
您在session_start()
. 将您的 PHP 代码放在 HTML 代码之上。