duanmei1946 2009-12-10 02:29
浏览 10
已采纳

会话和cookie在同一个PHP文件中?

Can't I set session and cookie in same PHP file?

I get an error message if I set the cookie after I've set session telling me that the header is already sent.

If I set session after cookie I get nothing but it seems not to work well.

  • 写回答

2条回答 默认 最新

  • doulan4939 2009-12-10 04:26
    关注

    The short answer is yes - you can set SESSION and COOKIE data in the same PHP file.

    The longer answer:

    • Cookie data is sent in the header of the page.
    • You can not set cookies after you have sent the headers to the client.
    • Headers will be sent as soon as you start outputting any data to the client (ie: the browser).

    It is likely that in your case, you have sent the header and/or started outputting data to the client in the same place you are setting SESSION data.

    See the PHP manual: Cookies for more details. In particular the quote of:

    "Cookies are part of the HTTP header, so setcookie() must be called before any output is sent to the browser. This is the same limitation that header() has. You can use the output buffering functions to delay the script output until you have decided whether or not to set any cookies or send any headers."

    If you need further help - try inserting your sample code/page that you are having issues with.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址