PHP Proper Logout Code

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

To provide a logout function, we have to destroy the session and expire the cookie (if cookies are being used to pass along the session identifier).  Based on the PHP manual, you have something like this…

Logout code

Logout code

Yes, the set_cookie function is a bit complex.  The reason you set $_SESSION to an empty array even though you are destroying the session is that session_destroy() is done upon garbage collection and not done immediately.   Setting to empty array, clears the session immediately.