phpdev
phpdev

Reputation: 11

php autologout when browser closed

session_set_cookie_params(0); is not working .if browser is closed ,site should be logout automaticaly. Please help me.

<?php
    session_set_cookie_params(0);
    session_start();
    include('connection.php');

    unset($_SESSION['email']);
    session_destroy();

    header('location:index.php');
?>

Upvotes: 1

Views: 53

Answers (1)

Khalid
Khalid

Reputation: 4808

You can do this using javascript, all you have to do is to delete a cookie named " PHPSESSID " which allows the server to know what is the session id of the client,

function removeCookie(cookieName)
{
    cookieValue = "";
    cookieLifetime = -1;
    var date = new Date();
    date.setTime(date.getTime()+(cookieLifetime*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = cookieName+"="+JSON.stringify(cookieValue)+expires+"; path=/";
}

This is the function that deletes the cookie, all you have to do is to call the function when the user closes the browser

window.onbeforeunload = closingCode;
function closingCode()
{
  removeCookie("PHPSESSID");
}

Upvotes: 2

Related Questions