In my app, I have a few forms that can be accessed from different urls in the app. Each form has a cancel button, that should return the user from the url that they previously came from.
So, this is pretty easy to achieve with $_SERVER[‘HTTP_REFERER’].
The problem comes into being when the user submits the form, and it fails validation. So, at that point, the $_SERVER[‘HTTP_REFERER’] is the form itself. Obviously, the cancel button wont work properly then, since the original referer has now been ‘lost’.
What is the best way around this? Should I be storing the original referer in the session, or perhaps via a hidden form field? Or perhaps there is another more elegant method?
You should not use the referrer for this, Use a different method. Maybe set some session variable yourself with ‘last visited page’ or use hidden field to save back page url.