0

I want to clear the php session array every time a user leaves my page, but my page has links with query strings. I don't want to clear the session array, when a user clicks on a link with a query string. I have tried the following javascript code but it does not work when the user leaves the page.

somepage.php

var url = new RegExp(/somepage.php\?sort=.*/);
if (url.test(document.location.href)){
    //do nothing
}
else {
    $(window).unload(function(){
        $.ajax({
            url: 'clear_session.php'
        });
    });
}
kingsolo
  • 79
  • 1
  • 1
  • 2
  • this question may help you http://stackoverflow.com/questions/1921941/close-kill-the-session-when-the-browser-or-tab-is-closed – JohnnyFaldo Aug 02 '13 at 14:19

1 Answers1

0

Calling a webserivce onunload is very unrealiable. Why not just unset the PHPSESSID cookie? This wont clean up the session on the server, but it will give the user a new empty session when he visits again.

Halcyon
  • 57,230
  • 10
  • 89
  • 128