Search code examples
phpsessionunset

Partially delete/destroy $_SESSION data? PHP


I am looking for a way to delete only certain amounts of SESSION data that is stored whilst preserving the session data associated with the user being logged on.

At the moment I am doing this by individual unset statements to the SESSION variables I want to delete.

However I was hoping there may be a more clever way to just delete a whole section of the SESSION array whilst preserving specific variables

e.g.

$_SESSION['username'];
$_SESSION['user_id'];
$_SESSION['ttl'];

The use case for this process would be:

User Logs In --> User performs task --> Once task is complete delete session data associated with task --> User is still logged in!

I had considered perhaps using a table in my Database monitoring logins, what are your opinions on that?

Thanks for your time!


Solution

  • There is no way to delete "whole section of the SESSION array whilst preserving specific variables".Instead of that you can use two dimensional array for a task and delete that array.

    $_SESSION["task1"]["username"] = "name"
    $_SESSION["task1"]["pass"] = "pass"
    
    $_SESSION["task2"]["name"] = "name";
    

    when task1 complete delete like

      unset($_SESSION["task1"]);
    

    now $_SESSION["task2"] still exist.