From what I understand of session variables in PHP, this variable lasts as long as a session lasts. Is there a possibility to overrule this and set a session variable to, let's say, 20 minutes (so that it is destroyed after 20 minutes of not using it)??

Joyce

    Not directly but you could do it via some code which you include at the top of every page (just after the session_start command).

    You could perhaps set a session variable with a name indicating a timeframe and store in that variable an array of session field names you want to be deleted?.

    various ways I can think to do what you need, another would be to just use a cookie rather than a session variable.

      Ehm, could you give me a hint, or a short example (eh, yes, I am a newbie, I come from ASP and am learning PHP now).

      Joyce

        Ok, not sure you need to do such a thing but I would probably try something like

        to set the purge time for session variables field1 and field2

        $x = time() + 1200;
        $_SESSION['auto_purge_'.$x] = array('field1', 'field2');
        

        and after the session_start on every page...

        foreach($_SESSION as $key => $value) 
        {
         if(substr($key, 0, 11) == 'auto_purge_')
         {
        	if(substr($key, 11) < time() )
        	{
        		foreach($_SESSION[$key] as $xkey => $xvalue) 
        		{
        			unset($_SESSION[$xkey]);
        		}
        		unset($_SESSION[$key]);
        	}
        }
        

        I have not tested any of the above but hopefully it will give you an idea on how to proceed.

          18 days later

          Argh, I completely forgot to thank you for this, so : THANK YOU! Sorry about that.

            No need to give it a second thought, but I assume if you are thanking rather than cursing me that you got it to work 🙂

            Glad to have helped.

              Write a Reply...