【问题标题】:Warning: session_start(): Cannot send session cookie - headers already sent by (BUT: No error on localhost)警告:session_start():无法发送会话 cookie - 标头已由(但:本地主机上没有错误)发送
【发布时间】:2018-10-23 03:04:04
【问题描述】:



我希望有人能帮助我解决这个有线问题。我的计算机上没有问题(Mac 上带有 XAMPP 的 localhost),但在我的虚拟主机上,我收到以下错误消息:

Warning: session_start(): Cannot send session cookie - headers already sent by...

我知道这个错误可能是由于调用之前的一些输出,但我想知道我是否可以在网络服务器或其他地方更改我的 PHP.ini (?) 上的某些内容以使其正常工作。

谢谢!
蒂姆

【问题讨论】:

    标签: php session localhost webserver


    【解决方案1】:

    这是写在php.ini 中的。很有趣...

    ; Output buffering is a mechanism for controlling how much output data
    ; (excluding headers and cookies) PHP should keep internally before pushing that
    ; data to the client. If your application's output exceeds this setting, PHP
    ; will send that data in chunks of roughly the size you specify.
    ; Turning on this setting and managing its maximum buffer size can yield some
    ; interesting side-effects depending on your application and web server.
    ; You may be able to send headers and cookies after you've already sent output
    ; through print or echo. You also may see performance benefits if your server is
    ; emitting less packets due to buffered output versus PHP streaming the output
    ; as it gets it. On production servers, 4096 bytes is a good setting for performance
    ; reasons.
    ; Note: Output buffering can also be controlled via Output Buffering Control
    ;   functions.
    ; Possible Values:
    ;   On = Enabled and buffer is unlimited. (Use with caution)
    ;   Off = Disabled
    ;   Integer = Enables the buffer and sets its maximum size in bytes.
    ; Note: This directive is hardcoded to Off for the CLI SAPI
    ; Default Value: Off
    ; Development Value: 4096
    ; Production Value: 4096
    ; http://php.net/output-buffering
    output_buffering=4096
    

    【讨论】:

      【解决方案2】:

      在您的php.ini 中,您可以为此将output_buffering 转为On。这应该可以防止标头发送错误,这样您的会话就不会失败。

      【讨论】:

      • 感谢您的提示。我检查了 phpinfo()。在网络服务器上,我有 output_buffering='no value',在我的本地主机上,我有 output_buffering=4096。这个值是多少?哪个值有意义?使用这个选项有什么缺点吗?
      • 最佳解决方案。谢谢。
      • 这就是我想看到的......所有其他关于这个问题的问题都得到了相同的响应——将 session_start() 移到文件的顶部——但没有解释它为什么起作用在我的本地主机上,但不在我们的网络服务器上。我仍然要将 session_start() 移到顶部,但现在我对我的环境有什么不同有了更好的了解。
      猜你喜欢
      • 2017-08-29
      • 2014-06-24
      • 2014-02-26
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 2018-01-29
      相关资源
      最近更新 更多