php start_session() 不适用于本地主机

发布于 2024-08-16 09:52:06 字数 935 浏览 4 评论 0原文

我刚刚开始制作这个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二手情话 2024-08-23 09:52:06

PHP 中的会话基于 cookie(用于传输 session_id)。

Cookie 作为 HTTP 标头发送。

仅当尚未发送任何输出时才能发送 HTTP 标头。

您收到的错误消息表明无法发送会话 cookie,因为某些输出已发送。

这意味着您必须确保在调用 session_start() 之前不发送任何输出(甚至没有一个空格!)。

几个想法:

  • 确保在开始的 php 标记之前没有空行,
  • 确保您的 PHP 脚本没有以 UTF-8 编码,并带有 BOM (某些编辑器默认将其放在文件的开头)
    • 即重新保存您的文件(作为 UTF-8,为什么不),不带 BOM

作为旁注:由于 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 :

  • make sure there is no blank line before the opening php tag
  • make sure your PHP script is not encoded in UTF-8 with a BOM (some editor put that by default at the beginning of the files)
    • i.e. re-save your file (as UTF-8, why not), without a BOM

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).

留一抹残留的笑 2024-08-23 09:52:06

很可能是字符编码问题(准确地说是 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?

我不在是我 2024-08-23 09:52:06

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文