(when user click the button link)
If the user is clicking a button, or anything for that matter, it could easily be a 'submit'. The submit could then be used to store the SESSION variable.
The key is the phrase "whenever the user leaves the page.
You could only store if the user leaves by way of the button.
POST the form to itself and then set the variable and use headers to redirect to another page.
razz