how to destroy cookies when the browser is closed? i have a php script where initially the user logs in and then the subsequent pages are displayed.i want that if the user closes the browser in between n tries to re-open the same page, he gets the error message that he has to login first (i.e the cookies which i have set once the user is logged in shud be destroyed once the user closes the browser.I am able to destroy my cookie after log out,but don't know how to destroy this when I will close the browser.

you can destroy the cookie by closing the browser if you set the cookie through javascript.

i have used php. is there any way to do in php?

You can use PHP and javascript together.I have never thought of solving your problem in PHP.

can you help me with the code in javascript? i am very new to javascript

i figured out a javascript to use with php, but here agn i am not sure where to place the code for erasing the cookies. also in my page there are links to other pages, so everytime i click on any link, the message pops up. how cn i avoid that?

<html>
<body>
 
<script language="JavaScript">
var needToConfirm = true;
 
window.onbeforeunload = confirmExit;
function confirmExit()
{
        if (needToConfirm)
                return "Are you sure you want to exit?";
}
</script>
</body>
</html>

if the cookie is set with time=0 it will expire when the browser is closed.
setcookie("name", "value", 0);

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.