Results 1 to 10 of 10

Thread: [RESOLVED] Session destroy when closing browser?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Resolved [RESOLVED] Session destroy when closing browser?

    Hi,

    How do I destroy a session when the browser window with my session data closes?

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Session destroy when closing browser?

    session_destroy() will, as you've probearly guesssed, destroys all the sesssion data. The problem only occurs when calling the function, but this can be resolved by calling an AJAX function or opening a new window to the location of your script by adding onUnload to the body tag or in javascript using

    Code:
    <script>
    window.onunload = function(){alert('Window Closed.')}
    </script>

  3. #3
    Fanatic Member
    Join Date
    Jul 2005
    Posts
    840

    Re: Session destroy when closing browser?

    Code:
    <?php
    	session_start();
    	
    	session_destroy();
    ?>

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Session destroy when closing browser?

    Ehm, how shall I use that? :P

  5. #5
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Session destroy when closing browser?

    create a script, like kenny_oh said, called destroy.php like

    PHP Code:
    <?php
        session_start
    ();
        
        
    session_destroy();
    ?>
    <script language="javascript" type="text/javascript">
        window.close();
    </script>
    and in your main script, the window which the user will be closing add the following script to it.

    PHP Code:
    <script language="javascript" type="text/javascript">
        
    window.onunload = function(){window.open('destroy.php');}
    </
    script

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Session destroy when closing browser?

    Cool, guys! Thanks. But:
    1) How can it be programmed so you can't see that window popping?
    2) Is it possible to make it purely in PHP so the user won't have to download java?

  7. #7
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Session destroy when closing browser?

    Its not possible to do it purely in PHP as PHP is server side and what you want is using client side. To do it without a window popping up then you need to use AJAX which creates a HTML request.


    Code:
    <script language="javascript" type="text/javascript">
    function createRequestObject()
    {
    	var request_o; //declare the variable to hold the object.
    	var browser = navigator.appName; //find the browser name
    	if(browser == "Microsoft Internet Explorer"){
    		/* Create the object using MSIE's method */
    		request_o = new ActiveXObject("Microsoft.XMLHTTP");
    	}else{
    		/* Create the object using other browser's method */
    		request_o = new XMLHttpRequest();
    	}
    	return request_o; //return the object
    }
    
    
    var http = createRequestObject();
    function destrorySession()
    {
    	try
    	{ 
    	http.open('get', 'destroy.php');
    	http.send(null);
    	}
    	catch(Exception)
    	{
    		http = createRequestObject();
    		destorySession();
    	}
    }
    
    window.onunload = destorySession();
    </script>

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    236

    Re: Session destroy when closing browser?

    Great!

    Now, I just found out that the pages logs you off when refreshing. Can that be disabled?

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Session destroy when closing browser?

    Hmm, this smells clunky to me. Are you aware that sessions time out automatically after a relatively short period of time anyway? There's not a lot of point in guesstimating when the user closes the window and destroying session data manually. There's not really a reliable way to tell the difference between closing the window and refreshing.

  10. #10
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Session destroy when closing browser?

    Is this what banks and other secure places use to make sure that all session information is clear in case the user is accessing the site from a public place, or where someone else is going to use the computer after them. Then they probearly wouldn't want to chance it that the session will time out before the next user arrives

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width