Surbhi Gupta
Surbhi Gupta

Reputation: 9

Can we unset specific session variable after some time codeigniter

I am setting a session variable using

$this->session->userdata(variable_name, value)

I want only this session variable unset after some time, i dont want to unset or destroy complete session, i want to unset this above set session variable after 30 minutes.

$this->session->unset_userdata(variable_name)

How can i do this?

Upvotes: 0

Views: 92

Answers (1)

SH_
SH_

Reputation: 266

One way could be to set a timestamp at the point where you set the session for variable_name. This would enable you to compare the current time with the time variable_name was set.

if (!isset(variable_name)) { // optional line if you want to stop the session from resetting
    $this->session->userdata(variable_name, value);
    $this->session->userdata(timestamp_variable_name, time());
}


// Convert 30 minutes into 1800 seconds ...
// Compare the current time with the time the variable was set ...

if (time() - strtotime($_SESSION['timestamp_variable_name']) > 1800)) {
   $this->session->unset_userdata(variable_name);
   $this->session->unset_userdata(timestamp_variable_name);
}

Upvotes: 1

Related Questions