views:

45

answers:

2

Code below:

$_SESSION = array();

Will it clear all session data? If I wouldn't want to use session_destory().

A: 

Yes, setting $_SESSION to a blank array will essentially unset all existing array keys.

Adam Backstrom
+3  A: 

Yup, it will destroy all session data but not the session itself.

Basically, there's three elements to a session:

  • The session itself, initialized with session_start()
  • The session cookie which is set automatically
  • Session data which is set via $_SESSION['foo'] = 'bar'

So you are only destroying the session data. session_destroy() destroys both the data and the session itself, but does not remove the session cookie.

The only "real" difference between $_SESSION = array() and session_destroy() is that after session_destroy(), setting session data will not work anymore before initializing a new session.

Tatu Ulmanen
It's akin to saying that $_SESSION = array(); will /reset/ the session whereas session_destory() /removes/ it.
Jon Cram