PHP - Session destroy after closing browser
The best way is to close the session is: if there is no response for that session after particular interval of time. then close. Please see this post and I hope it will resolve the issue. "How to change the session timeout in PHP?"
Destroy PHP Session on closing
if you use:
session_set_cookie_params(0);
session_start();
Your session cookie will destroy when the browser is closed... so your session will be good until they close the browser. IE. You login, and you are logged in, you close the browser, re-open it, go to the site again, and you wont be logged in.
Destroy or unset session when user close the browser without clicking on logout
You can set an expiration time for the session data, test it with each session_start
call and destroy the session if it’s expired:
session_start();
if (!isset($_SESSION['EXPIRES']) || $_SESSION['EXPIRES'] < time()+3600) {
session_destroy();
$_SESSION = array();
}
$_SESSION['EXPIRES'] = time() + 3600;
Session Destroy when one browser tab gets closed
Due to the nature of the client/server model there is no easy way to do what you want. If the user may stay idle for a while after loading your page, he can also close the tab and open it again. You have no control of what the clients do in the client side. There are simple javascript methods to do this such as the unload event, but they are not reliable and often don't work.
You may use websockets to ensure the client is always connected or do several ajax requests in the background and keep a timeout of a few seconds in your session, but those methods will disconnect the user if his internet connection drops even for a few moments.
PHP session destroy on closing the browser
Assuming you are using cookie-based sessions, set the timeout very aggressively.
http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime
You could also hook into the browser's unload event via JavaScript, and trigger a quick AJAX-request to your server that destroys the session.
http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-closed-event/
I wouldn't rely on this client-side implementation, though - if the browser crashes, or the user force-closes it, that may not trigger the event. The browser itself may also limit how long it will give an onunload
event to complete, in consideration of the user. A combination of these two methods would probably be the most effective.
Related Topics
How to Convert Output of Number_Format Back to Numbers in PHP
Add Columns to Admin Orders List in Woocommerce
How to Run the PHP Code Asynchronous
Use Strings to Access (Potentially Large) Multidimensional Arrays
Back to Previous Page with Header( "Location: " ); in PHP
How to Reduce the Image Size Without Losing Quality in PHP
Add a Prefix to Each Item of a PHP Array
Extract Text from Doc and Docx
How to Solve JSON_Error_Utf8 Error in PHP JSON_Decode
Laravel Preg_Match(): No Ending Delimiter '/' Found
Leverage Browser Caching for 3Rd Party Js
PHP Messing with HTML Charset Encoding
Symfony2 and Date_Default_Timezone_Get() - It Is Not Safe to Rely on the System's Timezone Settings