You would need to use javascript to tell the server when the user leaves the page. The webserver washes it's hands of the page once it leaves the server, while the user might keep the page open for a week.
If you use javascript on the page, when the page unloads to fire off a notice to your server you can take some action.
Although, you can't tell if he's leaving your page for another one of your pages, another website, or closing the browser.
And that last notice you fired isn't guaranteed to always be sent, so you can't rely on it completely.
What's the scenario you're looking for? There might be a better way to accomplish what you're trying to do.