Jump to content

Session with timeout, destroying session variables after 5mins, how to?


Vineet Sawant
 Share

Recommended Posts

Hello all,

I hope everyone's  having a nice weekend.

For a project, I need to store some user information in session variables. Right now I'm storing that information to temp_*page_name* pages for every user. But I need to store this information only for 5 minutes, if user completes payment within that time, those stored variables are then moved to database in user's history. If user fails to complete payment before that, the session must be destroyed.

I've no idea how to do this. I've read about $session in cheatsheet & PW's API documentation but no idea how exactly to do it.

If someone can guide me a bit, that'd be great. All I need to know is how to use PHP's session timeout with PW's $session.

Thanks in advance.

Link to comment
Share on other sites

Hi Vineet,

I have no knowledge about session_timeout. But when reading your post, another solution comes to my mind.

Why do you not store the temporary data into the users history pages? Somehow signed as "pending aproval" together with a timestamp?

Then there is no need for extra temp_pages or using & tweakin sessions. Maybe more secure, less data overhead and less coding.

This way the data must not be deleted after 5 minutes. Its enough that it is only valid for 5 minutes. You may delete such records when come along that users pages the next time (needs only a (few) line(s) of code I think).

EDIT: But if you want to delete those data by regular intervals, you may use lazy-cron running onto all "pending-aproval" pages.

Just my 2cents.

Edited by horst
  • Like 2
Link to comment
Share on other sites

It's perfectly fine not to use ProcessWire's $session functionality and just use $_SESSION. rya has said before it's fine to use one or the other, but pick one and don't try and use them both on the same page :)

I'd do something like this personally:

// Create a session variable called something like this after you start the session:
$_SESSION['user_start'] = time();
 
// Then when they get to submitting the payment, just check whether they're within the 5 minute window
if (time() - $_SESSION['user_start'] < 300) { // 300 seconds = 5 minutes
    // they're within the 5 minutes so save the details to the database
} else {
    // sorry, you're out of time
   unset($_SESSION['user_start']); // and unset any other session vars for this task
}

I would probably actually store it all in a multidimensional array though actually so it's easy to delete the whole thing:

$_SESSION['myapp']['user_start'] = time();
$_SESSION['myapp']['someuserinfo'] = "whatever";
 
// Then when you get past 5 minutes, if the user is still browsing the site you do
unset($_SESSION['myapp'];

It might even be better to have that in the top of the head.inc file to check the 5 minute window - that way if a user browses to another page the session time check is happening wherever they are on the site and cleaning itself up as it goes.

  • Like 2
Link to comment
Share on other sites

Hello horst & Pete,

Both of you have the solutions that pretty much get the job done for me.

Pete, thank you very much for explaining with code samples, makes it easy for not so bright people like me :P

Since my current site setup is already storing the details in temp pages, I think I can try what horst has suggested.

I've never used sessions before so will give it a try on a simple project first before trying on the current project.

Thanks again to both of you :)

Link to comment
Share on other sites

...

It might even be better to have that in the top of the head.inc file to check the 5 minute window - that way if a user browses to another page the session time check is happening wherever they are on the site and cleaning itself up as it goes.

^-^

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...