drju37335 2015-08-29 17:37
浏览 53

PHP会话有时无法重启

I have a PHP script which returns a receipt to a customer purchasing on my website. When they get the receipt, I want to be able to start a new session. So far I have a piece of code at the end of the script which returns the receipt page. It is:

if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(),
    '',
    time() - 42000,
    $params["path"],
    $params["domain"],
    $params["secure"],
    $params["httponly"]
    );
}

session_destroy();
session_regenerate_id(true);

When I use the website and go through the purchasing process, after the receipt page is served, the session id remains the same.

Yet, I took the above code and placed it in a seperate script called 'regenerate.php'. I then called this in another script, like so:

<?php
    include("regenerate.php");
    session_start();
    echo("<br>id:".session_id());

    include("regenerate.php");
    session_start();
    echo("<br>id:".session_id());

    include("regenerate.php");
    session_start();
    echo("<br>id:".session_id());

    include("regenerate.php");
    session_start();
    echo("<br>id:".session_id());
?>

When I run this script, then session id changes each time the regenerate script is run. However, the same code does not work in the intended page I am trying to serve up before restarting the session.

Is there any reason it might work in one case and not the other? I thought it might be because text is already being written out to output, however it happens in both cases.

  • 写回答

1条回答 默认 最新

  • dougouqin0763 2015-08-29 19:30
    关注

    You can't generate a new session during the same request, after sending output to the browser.

    Simply because the session cookie has already been sent - with the headers. So most likely your second call to session_start() gives an error.

    You can find more about turning display_errors On here: How do I get PHP Errors to display?

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line