session_destroy 不取消设置 session_id

发布于 2024-12-22 18:23:46 字数 707 浏览 1 评论 0原文

我正在开发一个在线订票系统,在成功预订后(付款后)我想清除会话 ID。但问题是,尽管我使用了 session_destroy() 来销毁会话,但我无法清除它。

注意:我已经回显了 session_id 以检查其是否重置。

网址:http://7sisters.in/7sislabs/

function book_final_tickets()
{

    //var_dump($_SESSION);
    $session_id = session_id();


    $sql = "
        UPDATE
            tbl_seat_book
        SET
            final_book = 'Y'
        WHERE
            session_id = '$session_id'
    ";


    //session_unset();

    if($r = $this->db->executeQuery($sql)){
        if(session_destroy()){
            unset($session_id); 
            echo 'Booking successfull';
        }
    }
}

I am working on an online ticket booking systems where after making successful booking(after payment) I want to clear the session id. But the thing is I am not able to clear it although I have used session_destroy() to destroy the session.

NB: I have echoed the session_id to check if its reset or not.

URL: http://7sisters.in/7sislabs/

function book_final_tickets()
{

    //var_dump($_SESSION);
    $session_id = session_id();


    $sql = "
        UPDATE
            tbl_seat_book
        SET
            final_book = 'Y'
        WHERE
            session_id = '$session_id'
    ";


    //session_unset();

    if($r = $this->db->executeQuery($sql)){
        if(session_destroy()){
            unset($session_id); 
            echo 'Booking successfull';
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

但可醉心 2024-12-29 18:23:46

session_destroy() 单独使用不会删除客户端 cookie,因此下次用户访问时,他们仍然会设置相同的会话 ID(但他们的服务器端会话信息将被被毁)。

来自文档(强调我的):

session_destroy() 销毁与当前会话关联的所有数据
会议。它不会取消设置与关联的任何全局变量
会话,或取消设置会话 cookie。 ……为了杀掉
整个会话,就像注销用户一样,会话 ID 也必须
未设置。如果使用 cookie 来传播会话 ID(默认
行为),则必须删除会话 cookie。

您可以使用 session_regenerate_id(true) 生成新的会话 ID 并删除旧会话 ID。请注意,这会将 $_SESSION 中的所有信息保留为新会话 ID 的一部分,因此如果您想清除会话信息,您仍然需要使用 session_destroy重新开始。

例如

<?php
    session_start();    
    $_SESSION['blah'] = true;

    var_dump(session_id()); // q4ufhl29bg63jbhr8nsjp665b1
    var_dump($_SESSION);    // blah = true

    session_unset();
    session_destroy();
    setcookie("PHPSESSID", "", 1); // See note below
    session_start();
    session_regenerate_id(true);

    var_dump(session_id()); // gigtleqddo84l8cm15qe4il3q3
    var_dump($_SESSION);    // (empty)
?>

,标头将显示客户端的会话 ID 更改:

请求标头
Cookie:PHPSESSID=q4ufhl29bg63jbhr8nsjp665b1

响应标头
设置-Cookie:PHPSESSID =已删除;过期=2010 年 12 月 27 日星期一 16:47:57 GMT
PHPSESSID=gigtleqddo84l8cm15qe4il3q3;路径=/

(您可以在此处不调用 setcookie() ,因为无论如何您都会创建一个新会话,因此 cookie 将被新 ID 覆盖,但这很好练习显式销毁旧的 cookie)。

session_destroy() alone won't remove the client-side cookie, so the next time the user visits, they'll still have the same session id set (but their server-side session info will have been destroyed).

From the docs (emphasis mine):

session_destroy() destroys all of the data associated with the current
session. It does not unset any of the global variables associated with
the session, or unset the session cookie. ... In order to kill the
session altogether, like to log the user out, the session id must also
be unset
. If a cookie is used to propagate the session id (default
behavior), then the session cookie must be deleted.

You can use session_regenerate_id(true) to generate a new session ID and delete the old one. Note that this will keep all of the information in $_SESSION as part of the new session ID, so you still need to use session_destroy if you want to clear the session info and start fresh.

e.g.

<?php
    session_start();    
    $_SESSION['blah'] = true;

    var_dump(session_id()); // q4ufhl29bg63jbhr8nsjp665b1
    var_dump($_SESSION);    // blah = true

    session_unset();
    session_destroy();
    setcookie("PHPSESSID", "", 1); // See note below
    session_start();
    session_regenerate_id(true);

    var_dump(session_id()); // gigtleqddo84l8cm15qe4il3q3
    var_dump($_SESSION);    // (empty)
?>

and the headers will show the session ID changing on the client-side:

Request Header
Cookie:PHPSESSID=q4ufhl29bg63jbhr8nsjp665b1

Response Header
Set-Cookie:PHPSESSID=deleted; expires=Mon, 27-Dec-2010 16:47:57 GMT
PHPSESSID=gigtleqddo84l8cm15qe4il3q3; path=/

(You can get away without the setcookie() call here, since you're creating a new session anyway, so the cookie will be overwritten by the new ID, but it's good practice to explicitly destroy the old cookie).

说好的呢 2024-12-29 18:23:46

使用 session_destroy() 销毁会话后,这对我有用:

setcookie('PHPSESSID',"",time()-3600,'/');

对我来说关键是将路径设置为“/”。这是真正摧毁饼干的唯一方法。

After destroying the session with session_destroy(), this worked for me:

setcookie('PHPSESSID',"",time()-3600,'/');

The key for me was setting the path to '/'. That was the only way to really destroy the cookie.

锦爱 2024-12-29 18:23:46

session_start之前调用session_id,并手动设置session_id

示例 1:将使用相同的 session_id

<?php
session_start();

echo session_id(); //4ef975b277b52

session_destroy();

session_start();

echo session_id();  //4ef975b277b52
?>

示例 2:手动设置 session_id(在 session_start() 之前调用)

<?php
session_id(uniqid()); 
session_start();

echo session_id(); //4ef975d3d52f5  (A)

session_destroy();


session_id(uniqid());
session_start();

echo session_id();  //4ef975d3b3399 (B)
?>

( A) != (B),所以你可以手动设置session_id,参见http://php.net/manual/en/function.session-id.php 了解更多信息。

另一个解决方案,不要使用 session_id() ,只需创建新的会话数组:

<?php
$_SESSION['booked'] = false;

if($r = $this->db->executeQuery($sql))
{
    $_SESSION['booked'] = true;
    echo 'Booking successfull';
}
?>

Call session_id before session_start, and set session_id manually .

Example 1: same session_id will be used

<?php
session_start();

echo session_id(); //4ef975b277b52

session_destroy();

session_start();

echo session_id();  //4ef975b277b52
?>

Example 2: set session_id manually (called before session_start())

<?php
session_id(uniqid()); 
session_start();

echo session_id(); //4ef975d3d52f5  (A)

session_destroy();


session_id(uniqid());
session_start();

echo session_id();  //4ef975d3b3399 (B)
?>

(A) != (B), so you can set session_id manually, see http://php.net/manual/en/function.session-id.php for more information.

Another solution, dont use session_id() , just create new session array:

<?php
$_SESSION['booked'] = false;

if($r = $this->db->executeQuery($sql))
{
    $_SESSION['booked'] = true;
    echo 'Booking successfull';
}
?>
A君 2024-12-29 18:23:46

试试这个:

unset($session_id);
session_destroy();

Try this:

unset($session_id);
session_destroy();
一向肩并 2024-12-29 18:23:46

而不是

session_destroy();

我宁愿只执行

session_regenerate_id(true);

并且您将获得一个新的 session_id

Instead of

session_destroy();

I'd rather do only a

session_regenerate_id(true);

and you will get a new session_id

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