php 警告:session_start():无法发送会话 cookie - 头已经发送(输出开始于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21521768/
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(): Cannot send session cookie - headers already sent by (output started at
提问by Geetha
The following warning comes in login page: Its working in localhost but not in remote host
登录页面中出现以下警告:它在本地主机中工作,但不在远程主机中
Warning:session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at on line 8)
警告:session_start() [function.session-start]:无法发送会话 cookie - 头已经发送(输出从第 8 行开始)
Warning:session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at on line 8)
警告:session_start() [function.session-start]:无法发送会话缓存限制器 - 已发送标头(输出从第 8 行开始)


index.php
索引.php
<?php
session_start();
if(isset($_SESSION['usr']) && isset($_SESSION['pswd'])){
header('Location: content.php');}
?>
<body>
<center>
<form method='post' action='login.php'>
<!– in this example I link it with login.php to check the password & username–>
<table>
<tr><td>Username:</td><td><input type='text' name='usr'></td></tr>
<tr><td>Password:</td><td><input type='password' name='pswd'></td>
</tr>
<tr><td><input type='submit' name='login' value='Login'></td>
<td><input type='reset' name='reset' value='Reset'></td></tr>
</table>
</form>
</center>
</body>
content.php
内容.php
<body>
<a href="resumedownload.php">Click here to Download to Resume</a>
<?php
session_start();
if(!isset($_SESSION["usr"]) || !isset($_SESSION["pswd"])){
header('Location: index.php');}
include 'logoff.php';
?>
</body>
login.php
登录.php
<body>
<?php
session_start();
if($_REQUEST['usr']=='suman.trytek' && $_REQUEST['pswd']=='solutions'){
$_SESSION['usr'] = 'suman.trytek';
$_SESSION['pswd'] = 'solutions';
header('Location: content.php');
}
else{
header('Location: index.php');
}
?>
</body>
回答by Krish R
Move the session_start();to top of the page always.
session_start();始终将移到页面顶部。
<?php
@ob_start();
session_start();
?>
回答by Frank
- session_start() must be at the top of your source, no html or other output befor!
- your can only send session_start() one time
- by this way
if(session_status()!=PHP_SESSION_ACTIVE) session_start()
- session_start() 必须位于源代码的顶部,之前没有 html 或其他输出!
- 您只能发送 session_start() 一次
- 这样
if(session_status()!=PHP_SESSION_ACTIVE) session_start()
回答by Spoutnik16
You cannot session_start(); when your buffer has already been partly sent.
你不能 session_start(); 当您的缓冲区已部分发送时。
This mean, if your script already sent informations (something you want, or an error report) to the client, session_start() will fail.
这意味着,如果您的脚本已经向客户端发送了信息(您想要的信息或错误报告),则 session_start() 将失败。

