php start_session() 不适用于本地主机
我刚刚开始制作这个 .php 文件,我似乎无法弄清楚为什么它会抛出这个警告
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php:1) in C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php on line 2
这是我的 php.ini 文件。
<?php
session_start();
require_once('../classes/users/BaseUser.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
...
</html>
这里是对我的 php.ini 文件的访问:
pastebin dot com / f60d22891 抱歉,我无法发布两个链接,因为一些垃圾邮件废话
我还发现这个线程表明我的权限设置被搞乱了。我尝试过这个...没有运气 PHP session_start 失败
I just started making this .php file and i cant seem to figure out why its throwing this warning
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php:1) in C:\Program Files\xampplite\htdocs\projectchest\controlpanel\index.php on line 2
Here is my php.
<?php
session_start();
require_once('../classes/users/BaseUser.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
...
</html>
And here is access to my php.ini file
pastebin dot com / f60d22891 sorry i cant post two links cuz of some spam nonsense
i also found this thread suggested that my permission settings are messed up. i tried this...no luck
PHP session_start fails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PHP 中的会话基于 cookie(用于传输
session_id
)。Cookie 作为 HTTP 标头发送。
仅当尚未发送任何输出时才能发送 HTTP 标头。
您收到的错误消息表明无法发送会话 cookie,因为某些输出已发送。
这意味着您必须确保在调用
session_start()
之前不发送任何输出(甚至没有一个空格!)。几个想法:
作为旁注:由于
output_buffering
:如果它打开(在 php.ini 文件中关闭),PHP 将保留一些数据在发送它们之前在内存中 - 这意味着即使少量数据似乎已经发送了,也可以发送标头(实际上,少量数据保存在内存中,而不是立即真正发送)< /em>.Sessions in PHP are based on a cookie (to transmit the
session_id
).Cookies are sent as HTTP headers.
HTTP headers can only be sent if no output has already been sent.
The error message you're getting indicates that the session cookie cannot be sent because some output has already been sent.
Which means you must be sure not to send any ouput (not even one white-space !) before calling
session_start()
.A couple of ideas :
As a sidenote : you might get this error on some servers, and not on some others, because of the configuration of
output_buffering
: if it's on (it's Off in your php.ini file), PHP will keep some data in memory before sending them -- which means headers can be sent even if a small amount of data seems to have already been sent (In reality, that small amount of data is kept in memory, and not immediatly really sent).很可能是字符编码问题(准确地说是 UTF 8 BOM)。尝试将您的 php 源文件转换为不带 BOM 的 UTF-8。您使用哪个编辑器?
Most likely a character encoding issue (UTF 8 BOM to be precise). Try converting your php source file into UTF-8 with no BOM. Which editor are you using?
php.ini 中还有
session.auto_start
指令 (参见手册) 一旦 PHP 发送 HTTP 标头,它将负责交换 cookie 并设置会话。不是可定制的,但它是距离最近的解决方案。There is also the
session.auto_start
directive in php.ini (see manual) which will take care of exchanging cookies and setting up the session as soon as PHP sends the HTTP headers. Not as customizable, but it's the least-distance solution.