I\'m trying to learn PHP and using sessions. I seen examples about using session_destroy on logout, but I see this in the php documentation:
In order
In my opinion only this is nesessary
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();**
At the first: use the unset() function. So if you have a $_SESSION['user_id'], and you wanne destroy it: unset($_SESSION['user_id'])
You can also use the unset() for cookies or other vars
Second: can we have some code?
There's, like, an example answering your question just below the paragraph you just quoted: http://php.net/manual/en/function.session-destroy.php
Example #1 Destroying a session with $_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's 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(); ?>
if you are using session id. then you can do this
session_start();
unset($_SESSION['id']);
session_destroy();
for cookies you can set the cookie time to some past date.
Only use session_unset() for older deprecated code that does not use $_SESSION.
session_start();
session_unset($_SESSION['email-user']); session_destroy();`
I've never actually deleted the session ID from the session. I usually just unset the necessary variables from the session (so if, for instance, you set a logged in user as $_SESSION['user'] = $userModel;
then I just unset($_SESSION['user']);
). To remove the cookie from the user's browser (like the documentation says) do this:
setcookie(session_id(), "", time() - 3600);
Read Example #1
from the session_destroy() documentation for more info.