Hi all,

Can anyone help me to solve my problem?

I m doing one PHP project in which i maximum used Javascript and Ajax.Now client require that when he close browser session have to automatically cleared means when he open next time that browser he must have to login first.

I tried to use javascript ONUNLOAD event of page but it cant work properly and one problem is that when i click on any other page that event occurs for current page which is also not good.

If you have any idea with php or javascript then please help me.

thanks in advance :-/

Hi all,

Can anyone help me to solve my problem?

I m doing one PHP project in which i maximum used Javascript and Ajax.Now client require that when he close browser session have to automatically cleared means when he open next time that browser he must have to login first.

I tried to use javascript ONUNLOAD event of page but it cant work properly and one problem is that when i click on any other page that event occurs for current page which is also not good.

If you have any idea with php or javascript then please help me.

thanks in advance :-/

How are you keeping the session? Are you using PHP's built in session management or a custom session management?

I believe session cookies (temporary cookies) should be cleared as soon as you close the browser. (those cookies without a date). I've never tested this though, and you could try to see if it actually happens.

Unfortunately, there is no easy way to know when the browser closes, or differentiate between a new page load, and closing the whole browser.

What you could do is on the onunload window event, make a XMLHttpRequest request to the server telling it to close the session in the next 10 seconds or so it there isn't another HTTP request made to it with the same session. If it is a page reload, then right after the XMLHttpRequest the server receives another HTTP request, which is the reload, and thus can cancel the request to cancel the session.
If the server doesn't receive another HTTP Request in 10 seconds, it can assume the browser was closed, and thus delete the session.

Another thing you could try, would be to try and make the HTTP Request persistent. Though HTTP is a stateless protocol, the underlying protocol TCP is session based and persistent. If you keep the HTTP connection open, essentially you're using the TCP session and can assume the browser is closed when the session closes.
To implement this you have to work around either the browser limitations, or the server limitations.
If you can control server limitations, then you can just make your server never time out the HTTP connection, and keep it sending new data. I believe this is called COMET and is implemented in a few HTTP servera, I don't know about Apache.
If you can't control the server, then you can try implementing either HTTP Binding, or HTTP BOSH, which are XMPP specifications that try to keep a persistent TCP connection in HTTP on Browsers.
http://www.xmpp.org/extensions/xep-0124.html
http://www.xmpp.org/extensions/xep-0206.html

To do this you need an Iframe to wrap your pages, so the Iframe keeps a persistent connection to the server. In the Iframe, either open a XMLHttpRequest or another child Iframe to a page on your server that will never finish loading, or that will implement HTTP Binding or BOSH.

hey,
i think if u r using php session then it is clear automatically when browser closed. means when it user opens the browser it goes to login page.but u can add this code to every page except login page.

<?php
if(!isset($_SESSION['sessionvalue'])){
?>
<script>
window.location('loginpage.php');
</script
<?php
}
?>

if it not help then tell me

Hi thanks digital-ether i will take a very brief look about your explanation and will also try when get time for same.

Thanks for your post nikesh but you not got my idea what i want

You can set directive in php.ini file to close the session when closed the browser
session.cookie_lifetime = 0;

commented: Useful post +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.