我刚刚开始制作这个 .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。
<?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 fails
最佳答案
PHP 中的 session 基于 cookie(用于传输 session_id
)。
Cookie 作为 HTTP header 发送。
只有在尚未发送任何输出的情况下才能发送 HTTP header 。
您收到的错误消息表明 session cookie 无法发送,因为某些输出已发送。
这意味着您必须确保在调用 session_start()
之前不发送任何输出(甚至连一个空格都没有!)。
一些想法:
- 确保开始 php 标记之前没有空行
- 确保您的 PHP 脚本未以 UTF-8 编码且带有 BOM (某些编辑器默认将其放在文件的开头)
- 即重新保存您的文件(作为 UTF-8,为什么不),不带 BOM
旁注:由于 output_buffering
的配置,您可能会在某些服务器上遇到此错误,而在其他服务器上则不会。 :如果它处于打开状态(在您的 php.ini 文件中处于关闭状态),PHP 将在发送数据之前将一些数据保留在内存中 - 这意味着即使少量数据似乎已包含头信息,也可以发送 header 已经发送(实际上,少量数据保存在内存中,而不是立即真正发送)。
关于php start_session() 不适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1961623/