How to exit php session - session_destroy seems not enough

I am trying to learn PHP and use sessions. I saw examples of using session_destroy when logging out, but I see this in the php documentation:

To kill a session altogether, as well as to log out, the session identifier must also be canceled. If a cookie is used to propagate the session identifier (default behavior), you must delete the session cookie. Setcookie () can be used for this.

so what should you do when you log out?

+1
source share
6 answers

, , , , : http://php.net/manual/en/function.session-destroy.php

# 1 $_SESSION

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>
+3

-,

session_start();
unset($_SESSION["nome"]);  // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();**
0

. (, , , $_SESSION['user'] = $userModel;, unset($_SESSION['user']);). cookie (, ), :

setcookie(session_id(), "", time() - 3600);

Example #1 session_destroy() .

0

-: unset(). , $_SESSION ['user_id'], : unset ($ _ SESSION ['user_id'])

unset() cookie vars

Secondly: do we have code?

0
source

if you use session id. then you can do it

session_start();
unset($_SESSION['id']);
session_destroy();

for cookies, you can set the cookie time to some past date.

0
source

Use only session_unset () for old legacy code that is not used by $ _SESSION.

session_start ();

session_unset ($ _ SESSION ['user email address']); session_destroy (); `

0
source

Source: https://habr.com/ru/post/1539098/


All Articles