简体   繁体   中英

Clear session on window close

I am working on a examination project, When user starts a test it open in new window by window.open(). If user close a test before it finish then session creates a problem so I want to clear session when user close the browser window or hide/disable close button before test completes.

nothing you come up with here will not be overrulable by the user; welcome to client-side programming (the actual browser "close" button is not within your reach). You can tap into onclose (lots of sites do this in order to pop up a confirm("do you want to leave this page?") dialog) but even that is very easy to bypass by anyone who knows how to open a browser console (F12 on every browser, for example) because they can just redefine window.confirm = (() => true); and not get bothered by confirm dialogs.

Partial solution would be to use the onbeforeunload and send a request back to the server once it is invoked.
Once the request is received on the server, you can destroy the session.
Again, this is easy to bypass on the client side but does provide extra functionality.

window.onbeforeunload = function() {
   // AJAX to server
};

You could use this answer here: https://stackoverflow.com/a/1119324/1489237

That would give you that message "are you sure you want to leave this page?"

On that function,instead of a message you can submit an ajax that will call a script to do a session_destroy() before closing the page.

I think you can set session expiration time to be few seconds? and perform ajax requests to maintain it, while student is answering questons

Can you use popup block with iframe inside?

<div id="test-popup">
    <iframe src="/test/12345">
</div>

And open it with JS:

$('#test-popup').show();

This is for example only. You need more CSS and JS to make it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM