dongsuiying7773 2012-04-28 06:59
浏览 143
已采纳

为什么`session_destroy()`在PHP中不起作用?

I have this problem with sessions in PHP which I have never been able to comprehend!
I have the following code in a PHP file:

session_start();
$sid = session_id();
session_unset();
session_destroy();
echo $sid;

Logically every time the it should give me different session IDs cause obviously I call session_destroy() but it just won't kill the session. It always gives me the same session ID!
Am I doing it wrong?

  • 写回答

2条回答 默认 最新

  • douquanzhan0315 2012-04-28 07:02
    关注

    For me, only the following combination works perfectly to destroy a session completely:

    session_destroy();
    session_unset();
    session_regenerate_id(true);
    

    Edit: Did you try this:

    <?php
    session_start();
    $sid = session_id();
    echo $sid;
    echo "<br />";
    
    //destroy session
    session_destroy();
    session_unset();
    
    //now start the new session
    session_start();
    session_regenerate_id(true);
    $sid = session_id();
    echo $sid;
    ?>
    

    It work's perfectly.

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

报告相同问题?