/    Sign up×
Community /Pin to ProfileBookmark

Destroy Session When browser is closed.

Hi Guys,

Is there a way to destroy session if browser/tab is closed?

Found this js online it works but if the refresh button in the browser is pressed it will destroy the session and logout the user, Is there any alternative for this? or is there a way to add a function that will detect if the refresh button is pressed?

var validNavigation = false;

function endSession() {
$.get(“logout.php”);

}

function wireUpEvents() {

window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}

$(document).bind(‘keypress’, function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});

$(“a”).bind(“click”, function() {
validNavigation = true;
});

$(“form”).bind(“submit”, function() {
validNavigation = true;
});

$(“input[type=button]”).bind(“click”, function() {
validNavigation = true;
});

}

$(document).ready(function() {
wireUpEvents();

});

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 23.2015 — If you specify an expiration time of "0" for the session cookie, that tells the browser to automatically expire it upon closing of the browser. That will not actually delete the session data on the server, but the (normal) user won't be able to access it until they log in again and get a new session cookie. That along with some "reasonable" session data retention time on the server is usually all you need. (see http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime )
Copy linkTweet thisAlerts:
@bordetaningauthorNov 24.2015 — Hi,

tried this code below but it's not working.



<?php

$cookie_name = "user";

$cookie_value = "me";

setcookie($cookie_name, $cookie_value, time() + (0), "/"); // 86400 = 1 day

if(!isset($_COOKIE)){

header("Location: logout.php");

} else {

echo "Cookie is set";

}

?>



Please help
Copy linkTweet thisAlerts:
@NogDogNov 24.2015 — If you want the session to end when the browser is closed....
[code=php]
<?php
// must be before every session_start()
// therefore a good thing to put in a config file
// that all your pages call first:
session_set_cookie_params(0);
session_start();

// rest of script...
[/code]

Or you just set it in php.ini or a local .htaccess file and don't worry about it.
×

Success!

Help @bordetaning spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...