Title: How to time out a session state variable in Yii?
Tags: php;session;yii
Question: I create a state variable in user identity class and and use in one of actions in controller. Before using I want to check if state variable exists. i.e

In userIdentity class:

```Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;setState('pictures', array());//Want this variable to die after 5 mins
```

In my controller action:

```if(isset(Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;pictures))
{

 //do what I want if state variable picture is set
}
```

Is there any way to set timeout for session state variables?
I know we can provide session timeouts in config file but that will logout user which is not I want. I just want to unset/destroy state variable 'pictures' after 5 minutues. 

Any way to do that in Yii?



Here is another answer: As I know there is no such built-in mechanism in Yii, so

on success login:

```Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;setState('pictures', array());
Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;setState('logged_time', microtime(true));
```

before each request

```if (Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;hasState('logged_time') 
&amp;&amp; (microtime(true) - Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;logged_time &gt; 300)) {
    Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;setState('pictures', null); // unset pictures state
    Yii181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16pp()-&gt;user-&gt;setState('logged_time', null); // unset logged_time if necessary
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:04/01/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><session><yii>', 'Title': 'How to time out a session state variable in Yii?', 'ViewCount': 1287, 'id': 22788749, 'user_AboutMe': '', 'user_AccountId': 3443912.0, 'user_DisplayName': 'Hammad', 'user_Reputation': 2031.0, 'user_Views': 279.0, 'user_id': 2885137.0}}