|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Kalamazoo, MI (USA)
Posts: 569
|
Dealing with POST data and expired sessions
Ok, so a client has discovered that when she leaves her add blog post page open long enough for the session to expire, and then tries to submit, she is logged out and her data lost. Needless to say, that's not desired.
I have some ideas about how to solve this, but I wanted to run it by all of you before I start coding. First of all, I'd like to use cookies to keep the user logged in for 30 days unless they request a shorter session at login (for when they are logging in remotely, library or internet cafe, whatever). Next, I'm thinking the best way to maintain that POST data is to throw it in the session. Because the session data is lost, I will probably need to store sessions in the database (more secure anyways, right?) and reload that session from a cookie value upon successful relogin. Can anyone think of a better way to handle this? |
|
|
|
|
|
#2 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Aug 2008
Location: The Netherlands
Posts: 525
|
Quote:
On a page of the website where the user can edit content like a blog entry you send an AJAX request to a very simple php script PHP Code:
If you send such a call every minute or every two miniutes or something it causes almost no data traffic at all and your client should stay logged in ![]() |
|
|
|
|
|
|
#3 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Kalamazoo, MI (USA)
Posts: 569
|
That's not always ideal (though probably better than our current setup). Sometimes you want the session logged out with a short period of inactivity. Using cookies will allow us to keep user data virtually as long as we want-- but it still doesn't protect against lost POST data.
I have seen on other sites, if I post data and them am redirected to a login page because my session expired, once I log in it tells me it completed the action I was trying to complete before it redirected. I would hope that it is somehow saving the action to be completed until after I log in, and not just completing the action and then telling me to log back in (thus opening up the script to fake POST requests that can modify data). |
|
|
|
|
|
#4 |
|
SitePoint Enthusiast
![]() Join Date: May 2002
Location: Melbourne, Australia
Posts: 73
|
I have never actually tried to implement this, but I think vBulletin handles this situation by somehow storing the submitted data, displaying the login form, logging the user in, then "resending" the submitted data again.
|
|
|
|
|
|
#5 |
|
SitePoint Mentor
![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,819
|
www-authentication solves all youor problems. If you don't want to use it as the primary login mechanism, you can fall back to it when your session based login fails.
|
|
|
|
|
|
#6 | |||||
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Kalamazoo, MI (USA)
Posts: 569
|
Quote:
Quote:
Quote:
Quote:
Quote:
|
|||||
|
|
|
|
|
#7 |
|
SitePoint Member
Join Date: Nov 2005
Location: Molde, Norway
Posts: 18
|
What if the login page receives the posted data (POST) and the URL it came from, and the login page leads her back to where she was with the data she submitted after successful login?
|
|
|
|
|
|
#8 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Location: Alkmaar, The Netherlands
Posts: 673
|
What you COULD do is make a javascript that requests a blank page with AJAX which has got session_start() on it, have the javascript execute the AJAX every 60 seconds and in theory this would result in the session never expiring ;-)
|
|
|
|
|
|
#9 |
|
SitePoint Mentor
![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,819
|
In that case, simply setting session.gc_maxlifetime to a higher value is probably a better solution.
|
|
|
|
|
|
#10 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2003
Location: UK
Posts: 934
|
Why not always ask for a username / password on a form?
If the user session is valid then it is optional. If it expires whilst the user has the form open, then treat like a validation error, and redisplay the form. |
|
|
|
|
|
#11 | |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,677
|
Quote:
For those suggesting AJAX, what would be the solution for users with mobiles and other devices likely to have JS off? (kyberfabrikken's solutions?) Often when I'm told to go log in again, I'll hit my back button, copy-pasta what I wrote, then go back forward and try logging in (this works because the POST didn't happen and instead I get a different URL sent to me, so my info isn't lost, but is still in my browser cache). Sometimes logging back in doesn't work due to server loads (was very common on a forum I am on), so I can always paste what I wrote in a text editor just-in-case. |
|
|
|
|
|
|
#12 | |
|
SitePoint Zealot
![]() ![]() Join Date: Feb 2006
Location: Netherlands
Posts: 174
|
Quote:
I do agree with allspiritseve though: always asking them for their username and password will confuse the users of my applications, and very understandable too, I think I'd be confused just as well as my end-users. I mean, while writing this reply, I wouldn't expect that I'd have to pass along my username and password, as it already says: "Logged in as webaddictz". I do, however, think there is a middle ground? When the form is submitted, check if there is a session, if there is not, save the information somewhere and redirect the user to a login page. After login, send them back to the form and let them submit again. No Ajax involved, and it will work on every browser. You could, of course, create an Ajax variant too. Upon submittal of the form, do an Ajax call to see if the session is still alive. If not, ask the user to fill out username and password, and submit that with form. On the serverside, log the user in and save the form's value(s). That actually sounds like a nice idea. |
|
|
|
|
|
|
#13 | |
|
SitePoint Mentor
![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,819
|
Quote:
What about you just show the form as usual. When the user submits it; if the session has expired you redisplay the form with the user + pass fields added to it (And the rest of the fields filled in from post data). That way, the normal flow would work unchanged, and you only do something different if the users session has expired. Basically, you treat the expired session as a validation error on the form, which is actually quite intuitive (IMHO). If you wanted, you could even redisplay the page with the original content in hidden fields, and only the new user + pass fields displayed. This gives an illusion about being redirected to a login page, when - in fact - there is no redirection going on. Since there is no actual redirect, you don't need to temporarily store the form contents in session or elsewhere. |
|
|
|
|
|
|
#14 | ||
|
SitePoint Zealot
![]() ![]() Join Date: Feb 2006
Location: Netherlands
Posts: 174
|
Quote:
![]() Quote:
Or is this something we shouldn't be afraid of? The only way the user + pass form entries will be shown is upon a POST, so a non-logged in user would have to guess the correct URL and submit a POST request which includes the values that are to be written and so he doesn't have the ability to override if he's not allowed to and can't read it because the GET would give an error. Gosh, you're right. I am over-complicating. This is a viable, secure and very user-friendly option and I'll be writing this in my newest applications from here-on now. By keeping the state at the client, and not the server, things do become less complicated, this is a very nice example of that. No Javascript magic, no complicating stuff like temporarily storing values, just another POST. Sounds darn good to me. |
||
|
|
|
|
|
#15 | |
|
SitePoint Mentor
![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,819
|
Quote:
|
|
|
|
|
|
|
#16 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Oct 2005
Location: Switzerland
Posts: 577
|
Hi
![]() Quote:
![]() |
|
|
|
|
|
|
#17 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Aug 2003
Location: UK
Posts: 934
|
Quote:
So if they post after their login credentials expire, just need the user to enter their password. |
|
|
|
|
|
|
#18 |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Jun 2006
Posts: 443
|
What you need to do is this:
#1 make sure all your forms post to them-selfs (302 to them-selfs on success, so you don't have the "refresh" confirmation popup) #2 - In case you have an "if (is not logged in) { redirect to login page; }" block in more than one place, put that in a function and call that function everywhere. #3 - In that function, before the actual redirect, check 2 things: a) If the request was GET, and if so, store the URL in a session variable (redirect to it after login) b) If the request was a POST, store the #1 the URL and #2 the posted data in the session. Redirect to that #1 URL after the login, and use the #2 data to pre-populate the form fields on the page. (Make sure to tie the data to the logged in user id or something) So, from the user's point of view: - login - go to edit profile - fill in profile - chase bag in the wind for 45 min (session is 15) - come back, submit the edit profile page --- get a "login here" page. - login --- get redirected to the submitted page, pre-populated with all the posted data, and some kind of message saying to repost, since the data was not saved cause of session time out. Hope that helps. |
|
|
|
|
|
#19 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Nov 2002
Posts: 845
|
A 12 hour session expiry works for a workday. They basically have to re-login the next day. If you leave the form overnight, do you really need to still be able to submit it?
I haven't implemented the solution to this myself, but we've using a ping to renew the session cookie. We've also used a count down timer to warn the user that they should submit soon, or at least go through a preview cycle to renew their session. |
|
|
|
|
|
#20 |
|
SitePoint Enthusiast
![]() Join Date: Apr 2009
Location: Lyon, France
Posts: 54
|
Hello,
Additionally, have your clients use Firefox and the Lazarus extension. Lazarus saved my life several times in the past few weeks. Well, not my life, but a few combined hours of work. |
|
|
|
|
|
#21 |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,677
|
So what's the Opera version of that, Florent? : )
|
|
|
|
|
|
#22 |
|
SitePoint Zealot
![]() ![]() Join Date: Feb 2006
Location: Netherlands
Posts: 174
|
|
|
|
|
|
|
#23 | |
|
Site Point Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2007
Location: Netherlands
Posts: 2,677
|
Quote:
My point wasn't how to do it in Opera, but that we often cannot tell clients to use browser-X with extension-Y except ion rare cases, so I was questioning it. It's okay to suggest it, but you have to still build for without it (unless this is internal application of course). |
|
|
|
|
|
|
#24 |
|
SitePoint Enthusiast
![]() Join Date: Feb 2004
Location: NJ
Posts: 96
|
We invoke session_start() on every page, test for a login cookie on every page that processes form data, and ALL form pages POST back to themselves. If the cookie test fails, we save the current URL and POST as session data, re-direct to a login page, and finally return from whence the user came to the saved URL. Each form tests for POST session data and uses that if found; that is, we restore POST from the saved session data, then remove the session data. It seems to work.
|
|
|
|
|
|
#25 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2008
Posts: 4,770
|
You don't necessarily have a problem with the back button if you don't redirect. This is more an effect of certain http headers being sent(primarily certain cache-control headers). btw, session_start() sends cache-control headers in addition to the potential session cookie header.
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 07:15.













Hybrid Mode
