TiMESPLiNTER
TiMESPLiNTER

Reputation: 5889

Is session_unset() deprecated?

In the PHP documentation for session_unset() there is no hint that this function is deprecated so I think it's fine to use it. But then I read through the documentation about session_destroy() where I found this hint:

Note: Only use session_unset() for older deprecated code that does not use $_SESSION.

I know that the session_unset() function is an equivalent for $_SESSION = array();. So what should I use now? Why is on one site a hint that this function is deprecated but on the other hand in the documentation about the function itselfs there is not deprecated note. What's the truth about this function now?

Upvotes: 9

Views: 1604

Answers (2)

Daniel W.
Daniel W.

Reputation: 32280

I don't know the exact reason, too, but it can be found here I guess: https://github.com/php/php-src/blob/master/ext/session/session.c

PHP handles variables in ZVAL pointers and I think they just wanted the _SESSION superglobal to be handled the same way as any other variable, and not with a "special" command session_unset().

Another benefit is better garbage collection handling I think.

Sometimes, "deprecated" does not mean its a bad function, but you should not use it because code might be removed in future packages simply because it is not neccessary.

Upvotes: 3

If you want to close the session you must use session_destroy().

session_destroy();

If you want to clear the variables of the session you must use:

$_SESSION = array();

And if you want to clear only one variable of the session you must use:

unset($_SESSION['example']);

Upvotes: 1

Related Questions