Logout of PHP session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Logout of PHP session

Post by Zoram »

How do you script the logout for a session?
User avatar
mr_griff
Forum Commoner
Posts: 64
Joined: Tue Sep 17, 2002 11:11 am
Location: Bozeman, Montana

Post by mr_griff »

Code: Select all

<?php
session_start();
session_destroy();

//User is logged out now
?>
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

or you could use

Code: Select all

<?php
unset($_SESSION);
?>
or

Code: Select all

<?php
session_unset();
session_destroy();
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Re: Logout of PHP session

Post by twigletmac »

Zoram wrote:How do you script the logout for a session?
It depends on how you set your session up in the first place and what version of PHP you have.

The old way (if you're using PHP 4.0.6 or lower):

Code: Select all

<?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_unset();
// Finally, destroy the session.
session_destroy();

?>
Using $_SESSION (if you have PHP 4.1 or up):

Code: Select all

<?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();
// Finally, destroy the session.
session_destroy();

?>
Examples taken from the manual.

Mac
Post Reply