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
2010-09-28 09:03:06
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
2010-09-28 09:06:05
Maybe it uses the HTTP Referrer from the HTTP Request headers sent by the user?
Andrew Dunn
2010-09-28 09:14:42
As far as I know the HTTP_REFERER variable is not fully reliable, am I wrong? Can you provide some code?
Kupido
2010-09-28 09:37:10
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
2010-09-28 09:43:10
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
2010-09-28 10:13:16
A:
Maybe the page with the form doesn't have any "unset" session variable but the others do ?
Chouchenos
2010-09-28 09:31:24
How would this work with multiple forms in different pages (or even in the same page)?
Kupido
2010-09-28 09:40:24
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
2010-09-28 09:46:50
Indeed.But if the site uses Zend Framework, it can use namespace (session) to do this in a very "clean" way.
Chouchenos
2010-09-28 10:07:51
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
2010-09-28 10:21:25