|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Understanding and Using Sessions
By: FYICenter.com
Part:
1
2
3
4
5
(Continued from previous part...)
How To Close a Session Properly?
Let's say you site requires users to login. When a logged in user clicks the logout button,
you need to close the session associated with this user properly in 3 steps:
- Remove all session values with $_SESSION = array().
- Remove the session ID cookie with the setcookie() function.
- Destroy the session object with the session_destroy() function.
Below is a good sample script:
<?php
session_start();
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
session_destroy();
print("<html><pre>");
print("Thank you for visiting FYICenter.com.\n");
print(" <a href=login.php>Login Again.</a>\n");
print("</pre></html>\n");
?>
What Is session_register()?
session_register() is old function that registers global variables into the current session.
You should stop using session_register() and use array $_SESSION to save values into the current session now.
Part:
1
2
3
4
5
|