Deleting sessions and variables

Deleting sessions and variables

In PHP, you can delete a session using the session_destroy() function. This function destroys all session data associated with the current session and removes the session ID cookie from the browser.

Here’s an example of how to delete a session in PHP:

<?php
// Start a new session
session_start();

// Set session variables
$_SESSION["username"] = "john";
$_SESSION["age"] = 30;

// Destroy the session
session_destroy();
?>

In this example, the session_start() function is called to start a new session. The $_SESSION superglobal array is then used to set two session variables, “username” and “age”, to the values “john” and 30, respectively. Finally, the session_destroy() function is called to destroy the session.

Note that session_destroy() only deletes the session data for the current session. If you want to delete a specific session variable, you can use the unset() function:

<?php
// Start a new session
session_start();

// Set session variables
$_SESSION["username"] = "john";
$_SESSION["age"] = 30;

// Delete a session variable
unset($_SESSION["username"]);
?>

In this example, the session_start() function is called to start a new session. The $_SESSION superglobal array is then used to set two session variables, “username” and “age”, to the values “john” and 30, respectively. Finally, the unset() function is used to delete the “username” session variable.

Note that unset() only deletes the variable from the current session, but does not destroy the entire session. To destroy the entire session, you still need to call session_destroy().

Share this post
[social_warfare]
Setting up the ListView
Custom session handlers in PHP

Get industry recognized certification – Contact us

keyboard_arrow_up