How to manage a session

Get the name of the session

cookie $name = session_name(); // By default, PHPSESSID

Get the value of the session ID

$id = session_id(); // For example, l1jef1foi1g8u6qnui4f8b6e14

Set the session ID

session_id('abc123');

How to end a session

$_SESSION = array(); // Clear session data from memory
session_destroy(); // Clean up the session ID

Delete the session cookie from the browser

$name = session_name();  // Get name of session cookie
$expire = strtotime('-1 year');  // Create expire date in the past
$params = session_get_cookie_params();  // Get session params
$path = $params['path']; 
$domain = $params['domain']; 
$secure = $params['secure']; 
$httponly = $params['httponly']; 
setcookie($name, '', $expire, $path, $domain, $secure, $httponly);

Description

Back