views:

44

answers:

2

Hi all, I've seen this behaviour in many websites and web applications but I'm not able to find a "clean and standard" way to reproduce it: the user fills in the form, submits it and then, after a successful validation, the form is reloaded with a message on top saying something like "The item has been saved". No problems so far, what I can't understand is how they keep displaying the confirmation message if that specific page is reloaded, but when the user goes to another page and then returns to the original one (the one containing the form) the message is not there anymore. There seems to be no get or post data, so I'm assuming session variables or cookies are used instead, but how do they know when to keep and when to unset them? Or maybe I'm wrong and there is some other way... help me, please!

+1  A: 

My guess would be that they used a session variable which unsets itself the moment that page is accessed. So if they try to access it again, they are simply redirected.

Andrew Dunn
This doesn't explain how the message is still displayed when you reload the page. If you unset the session variable, the message should not be displayed again.
Kupido
Maybe it uses the HTTP Referrer from the HTTP Request headers sent by the user?
Andrew Dunn
As far as I know the HTTP_REFERER variable is not fully reliable, am I wrong? Can you provide some code?
Kupido
HTTP_REFERER is always reliable for web browsers where the users haven't been screwing with the settings: `$_SERVER['HTTP_REFERER']` will fetch it in PHP
Andrew Dunn
Maybe I could use a session variable to store the form and its url and then check the url against the current one. If they match, than the message should be displayed, else the session variable should be unset.
Kupido
A: 

Maybe the page with the form doesn't have any "unset" session variable but the others do ?

Chouchenos
How would this work with multiple forms in different pages (or even in the same page)?
Kupido
The session variable doesn't have to be the same for each form validation. For example, you can use $_SESSION['registrationFormValidation'] for registration form and $_SESSION['contactFormValidation'] for the contact form. You can, then, unset the first one on the contact page and vice versa.
Chouchenos
Yes, this could be a solution but it doesn't seem so "clean"... :)
Kupido
Indeed.But if the site uses Zend Framework, it can use namespace (session) to do this in a very "clean" way.
Chouchenos
Really? My site is not based on Zend Framework but I think I'll have a look at Zend_Form inner workings. Thanks for your suggestion.
Kupido