Ending a Session

When the time comes to end a session, usually when a user requests to log out from your site, you can use the session_destroy function in association with the unset function, as in following Example. That example provides a useful function for totally destroying a session, logging a user out, and unsetting all session variables.

<?php
function destroy_session_and_data() 
{
	session_start ();
	$_SESSION = array ();
	if (session_id () != "" || isset ( $_COOKIE [session_name ()] ))
		setcookie ( session_name (), '', time () - 2592000, '/' );

	session_destroy ();
}
?>