如何重置我的会话ID?

Ass*_*Guy 3 php session sessionid

他付款成功后,我正试图取消会议。

 $session_id = session_id();

    $sql = "
        UPDATE
            tbl_seat_book
        SET
            final_book = 'Y'
        WHERE
            session_id = '$session_id'
    ";
    $r = $this->db->executeQuery($sql);

        session_unset();
        session_destroy();

    echo 'Booking successfull';
Run Code Online (Sandbox Code Playgroud)

但是当我订票的时候,同样session_id出现了。session_id付款成功后如何销毁。

编辑:

$sql = "
        INSERT INTO
            tbl_seat_book
        SET
            booking_date = '$_REQUEST[bus_date]',
            booking_time = '$_REQUEST[bus_time]',
            bus_id = '$_REQUEST[bus_id]',
            route_id = '$_REQUEST[route_id]',
            session_id = '$session_id',
            session_start_time = '$time',
            temp_book = 'Y',
            final_book = 'N',
            $cond
    ";
    $booked = $this->db->insertQuery($sql);
Run Code Online (Sandbox Code Playgroud)

编辑2:

function book_final_tickets()
{

    $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';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ams 5

使用session_regenerate_id(true)生成一个新的会话ID,并删除旧的。请注意,这会将所有信息保留$_SESSION为新会话ID的一部分。

例如,要获取新的会话ID,但将会话信息保留在中$_SESSION

// Regenerate session ID, user gets a new sid, and cookie is updated.
session_regenerate_id(true);

// Make sure you pick up the new session ID
$session_id = session_id();

// Note, $_SESSION will be the same here as before, but $session_id will be diff.
// Old session info has been moved to new session.
Run Code Online (Sandbox Code Playgroud)


如果您不想同时保留任何信息$_SESSION,那么您想使用它session_destroy()来破坏服务器端的会话信息,并setcookie()在客户端手动取消设置会话ID cookie。然后,您可以像以前一样开始新的会话并生成新的会话ID。

例如,要获取新的session_id并删除所有会话信息。

session_unset();   // Remove the $_SESSION variable information.
session_destroy(); // Remove the server-side session information.

// Unset the cookie on the client-side.
setcookie("PHPSESSID", "", 1); // Force the cookie to expire.

// Start a new session
session_start();

// Generate a new session ID
session_regenerate_id(true);

// Then finally, make sure you pick up the new session ID
$session_id = session_id();

// $_SESSION will now be empty, and $session_id will have been regenerated.
// You have a completely empty, new session.
Run Code Online (Sandbox Code Playgroud)

setcookie()由于您仍在创建新的会话,因此您无需拨打电话就可以离开,因此cookie将被新的ID覆盖,但是明确销毁旧的cookie是一个好习惯)。


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

从文档(重点是我的):

session_destroy()销毁与当前会话关联的所有数据。它不会取消设置与该会话关联的任何全局变量,也不会取消设置会话cookie。...为了完全终止会话,例如要注销用户,会话ID也必须未设置。如果使用cookie传播会话ID(默认行为),则必须删除会话cookie。