[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 713
  • Last Modified:

PHP Persisting vars on session after long process

Hi Experts,

I need to solve the following situation:

I have some vars stored on the session ( some ids by example ), but after one long process (about 1 hour), the session variables values are lost.
Can you give me some tip?

I'm using Yii, but i think is basically a php problem.
0
MarianoSB
Asked:
MarianoSB
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
Julian HansenCommented:
I have to ask - are you sure you want a php script running for an hour - what happens on the client side ?

Having said that - you can store the information in a cookie - that won't be lost. Or save an identifier in a cookie and put the vars in a database.
0
 
Marco GasiFreelancerCommented:
I think that is an admin script. If this is the case try to use following line of code at the top of your script:

ini_set(’session.gc_maxlifetime’, 60*60); // 1 hour = 60 seconds * 60 minutes = 3600 seconds

If this is not an admin script, I have simpathy for your user ;-)

Cheers
0
 
Dave BaldwinFixer of ProblemsCommented:
The normal time-out for a PHP session is 1440 seconds or 24 minutes.  This page http://www.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime says that the sessions with the Shortest lifetime will determine the time-out so setting a longer time-out on one session won't help.
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Marco GasiFreelancerCommented:
Hi, Dave. Let me ask a question about this: if you include that directive in a separate file and then iclude that file in any script of your site, can you bypass the problem?
0
 
Julian HansenCommented:
Often modifying the ini settings on hosted sites is disallowed by ISP's hence the recommendation that you use a cookie as this is not dependent on server settings. And if it is an admin script and / or you are not in the EU cookies should not be a problem
0
 
MarianoSBAuthor Commented:
Thanks you all for posting,

You are right with the assumption, It's an admin script.
I'm trying with
ini_set(’session.gc_maxlifetime’, 60*60), but doesn't seems to solve the problem.
I would prefer not to use cookies, at least by now, because that would imply a lot of re-coding. Is there any consideration to take care of, about cookies and ajax?
Regards,

Mariano
0
 
Dave BaldwinFixer of ProblemsCommented:
According to that link, that problem will occur with any routines on the server that use that 'session-save' directory.  The shortest time-out will clear the entries that exceed it's timeout setting.  If this is shared hosting, you can not bypass the problem.  If it is not shared hosting, the 'php.ini' file can be changed to an hour.

I think that because of all the complications involved in extending a session to save data, you should consider using a table in a database instead.  That could be a problem too if multiple instances of the admin script can be run although there are ways around that.
0
 
Ray PaseurCommented:
You can set the session cookie yourself.  You can set any expiration for the session cookie, including a non-zero value.  The zero value or any value less than time() will cause the session cookie to expire at the end of the browser life.

// START THE SESSION AND SET THE COOKIE FOR ALL SUBDOMAINS
$sess_name = session_name();
if (session_start())
{
    // MAN PAGE http://php.net/manual/en/function.setcookie.php
    setcookie
     ( $sess_name
     , session_id()
     , NULL                // THIS IS WHERE YOU CAN SET THE TIME
     , DIRECTORY_SEPARATOR
     , $host
     , FALSE
     , TRUE
     )
     ;
}

Open in new window

However I do not think the cookie is the problem.  I think it is session garbage collection that is occurring.  You might want to write your own code for this...
http://php.net/manual/en/function.session-set-save-handler.php

See also:
http://us.php.net/manual/en/session.configuration.php#ini.session.gc-probability
http://us.php.net/manual/en/session.configuration.php#ini.session.gc-divisor
http://us.php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime
0
 
Julian HansenCommented:
I don't see that cookies need that much recoding - you can initialise a Session from the cookies each time the page is openend - so your code still uses the session array - it just does not have to persist across actual sessions.
0
 
MarianoSBAuthor Commented:
Hi Experts,
I found a possible solution here:

http://blog.natesilva.com/post/250569350/php-sessions-timeout-too-soon-no-matter-how-you-set

my development machine and my server runs Ubuntu, and when i changed my php.ini locally, the garbage collection period was recognised.
On the other hand, i agree that cookies may be the correct way to go.

Thank you all for posting and please excuse my english. Regards,
Mariano
0
 
Ray PaseurCommented:
Hope I can save you some trouble.  The life of the cookie does not matter if you set the cookie life to a value less than the value of time().  No matter what value you set, the cookie will expire at the end of the browser.  This means all instances of the browser - not just the current tab or windows.

Best of luck with your project, ~Ray
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now