Page 180 - Open Soource Technologies 304.indd
P. 180
Unit 10: Cookies
10.5 Destroying Session and Unsetting Variables Notes
You can use session_destroy() to end a session, erasing all session variables. The session_
destroy() function requires no arguments. You should have an established session for this
function to work as expected. The following code fragment resumes a session and abruptly destroys it:
session_start();
session_destroy();
When you move on to other pages that work with a session, the session you have destroyed
will not be available to them, forcing them to initiate new sessions of their own. Any registered
variables will be lost.
The session_destroy() function does not instantly destroy registered variables, however.
They remain accessible to the script in which session_destroy() is called (until it is reloaded).
The following code fragment resumes or initiates a session and registers a variable called test,
which we set to 5. Destroying the session does not destroy the registered variable.
session_start();
$_SESSION[test] = 5;
session_destroy();
print $_SESSION[test]; // prints 5
To remove all registered variables from a session, you simply unset the variable:
session_start();
$_SESSION[test] = 5;
session_destroy();
unset($_SESSION[test]);
print $_SESSION[test]; // prints nothing.
eaders are pieces of information sent to the browser before the main page is
evaluated. When a cookie is sent, it must be accompanied by a compact privacy
Hpolicy so the user’s browser can look at both, see if they marry up, and decide
what to do. Get this bit right, and all but the toughest setting on your user’s browser won’t
have a problem with your cookies.
Now, we don’t need to go through the details of this, because the good folks at the Privacy
Council offer an automated service that creates compact policies. They’ll even email the result
to you. Just register with them, select from a series of multiple choice questions about what
your site does and doesn’t do, and you’re in business again.
Contd...
LOVELY PROFESSIONAL UNIVERSITY 175