unset php session code example

Example 1: php unset session variable

// Destroy a sesion variable name 'variable_name' 
<?php
unset($_SESSION['variable_name']);
?>

Example 2: php delete session

session_destroy(); // To delete whole session
// OR
unset($_SESSION['myVar']); // To delete a session var

Example 3: php session destroy

<?php
// Destroy the currently active session.
session_destroy();
?>

Example 4: php clear session

<?php
session_destroy();
?>

Example 5: session value not removed php

All of a sudden neither session_destroy() nor $_SESSION=[] were sufficient to log out. I found the next to work:
<?php
setcookie(session_name(), session_id(), 1); // to expire the session
$_SESSION = [];
?>

Tags:

Php Example