0

I wonder if i've unset($_SESSION['enough']); and want to free it up on closing the page. [ suppose visitor is viewing page of the website pages in new tab ]

i'm using this code

<script language="javascript">
window.onbeforeunload = function() {
console.log('event');
return false;
}
</script>

i wonder how can i apply to fire this code unset($_SESSION['login_id']); , it might look ridicules but this is the basic idea and i'm gonna give example what can be used for

For example : media website would like members not to watching more than one video in same time so the watching page drop session and free it on closing it so can watch more! js indeed is essential for website using jwplayer so no chance of talking about members with disabled js.

Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71

2 Answers2

1

In order to load the killsession.php that runs the unset() command, you can run that page with ajax with async:false

Have a look at Ajax request with JQuery on page unload

jQuery.ajax({url:"http://localhost/killsession.php", async:false})
Community
  • 1
  • 1
zurfyx
  • 31,043
  • 20
  • 111
  • 145
0

You can use jQuery to fire on unload with the unload function (http://api.jquery.com/unload/).

$( window ).unload(function() {
    // On the unload, we can fire a request back to the server
    // .get(), .post(), and .ajax() may be useful.
});
Julio
  • 2,261
  • 4
  • 30
  • 56
  • thanks for sharing your answer but i wonder if we suppose we've file `killsession.php` with code `unset($_SESSION['enough']);` how rather than the `alert( "Bye now!" );` we execute it to free the session on closing the page. – Reham Fahmy Dec 17 '13 at 19:28
  • 1
    You have the right idea: Make a call to `killsession.php` using one of `.get()`, `.post()`, or `.ajax()`. Once the user accesses that page, their sessions will be updated. – Julio Dec 17 '13 at 19:31