2010-11-10 101 views
0

我不能开始使用session_start()会议......我得到这个错误:PHP会话问题

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /**************/index.php:5) in /**************/session.php on line 2 

这是我的session.php文件文件的内容:

<?php 
session_start(); 

if(((!isset($_SESSION['user']))) || (!isset($_SESSION['valid'])) || ($_SESSION['valid'] != -1 && $_SESSION['valid'] != 0 && $_SESSION['valid'] != 1)) 
$_SESSION['valid'] = 0; 

function destroy_session() 
{ 
    session_destroy(); 
} 
?> 

,我需要迫切解决这个问题!

+0

你确定''<?php'之前没有空白吗? – 2010-11-10 23:53:37

+0

http://stackoverflow.com/questions/1183726/headers-already-sent-in-php – Phil 2010-11-10 23:54:18

+0

这个文件是否被另一个文件包含? – lonesomeday 2010-11-10 23:55:27

回答

2

错误消息告诉您输出开始

output started at /**************/index.php:5 

要么调用session_start()发生在输出之前(最好),或使用输出缓冲(不太优选)

编辑:如果你不是意识到这个错误背后的原因,请阅读PHP header()manual page

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

1

听起来像是你需要在您的代码中早先包含该文件。主要是在输出任何HTML或内容之前。