views:

451

answers:

3

I'm doing a simple chat script and I have the killSession function which kills the current session and deletes the user from the DB. The problem is that after the name is set and verified the chat form doesn't load, it just kills the session and goes back to the loginForm(). Here's the script:

<?php
if (!isset($_SESSION['name'])) {
 loginForm(); // set a name for chat
} else {
?>
 // chat form

 $(window).unload(function () {
  <?php  killSession(); ?>
 });

Is there a way to trigger killSession() only after I refresh or close the page?

UPDATE:

Still can't figure it out. Maybe I need a break.

$(window).unload(function () {
 $.get("killSession.php", { name:"test" }); // i set the name to test for testing
});

Here's the killSession.php page:

session_start();
function killSession($name) {
 include("config.php");
 mysql_query("DELETE FROM sessions WHERE name='$name'");
 session_destroy();
 header("Location: index.php");
}

killSession($_GET['name']);

The $.get still doesn't work, so I tried it separatly via the browser. killSession.php?name=test will delete from the DB, but won't kill the session.

+5  A: 

The session is killed immediately when the PHP function killSession is called. So your code will kill the session whenever it’s executed.

What you need to do is to call the PHP function killSession only when the the onunload event is fired. And calling a PHP function out of JavaScript is only possible with sending a new request to the server.

So you need to send a request to a script that then calls the killSession function, maybe something like this:

$(window).unload(function () {
    $.get("killSession.php");
});

And inside the killSession.php you then call the PHP function killSession.

Gumbo
+1 - simple idea, yet I probably never would have thought of it.
karim79
It doesn't seem to work. I put an alert inside the unload to check if there's a problem with that, but I think it's having trouble getting the call from the page.
Norbert
How do you pass the session ID along? If it’s per URL, then you need to add the session ID to the URL in that JavaScript code.
Gumbo
Still can't figure it out. Maybe I need a break.$(window).unload(function () { $.get("killSession.php", { name:"test" }); // i set the name to test for testing});Here's the killSession.php page:function killSession($name) { include("config.php"); mysql_query("DELETE FROM sessions WHERE name='$name'");// session_destroy(); - doesn't work, unset doesn't do the job unset($_SESSION['$name']); header("Location: index.php");}The $.get still doesn't work, so I tried it separatly via the browser. killSession.php?name=test will delete from the DB, but won't kill the session.
Norbert
Crap.. Sorry. I added the code in the OP.
Norbert
Try setting $_SESSION['$name'] = '';
Sohnee
A: 

i would recommend hidden iframe calling PHP kill session script or AJAX HTTPrequest GET method

dusoft
+1  A: 

The request is not working because it is async this means that you're javascript does not have to wait for the server. Making the request synchronous will make the javascript wait for a response:

$(document).ready(function()
{
    $(window).unload(function() 
    { 
        $.ajax
        ({
            type: "GET",
            url: "killSession.php",
            async: false,
            data: "name=test",
            success: function(msg)
            {
                alert(msg);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                alert("I failed");
            }
        });
    });
});

On ajax:

I'm assuming that you think that whenever you call Location: index.php in php (per ajax).That the browser will redirect there. Ajax doesn't work this way in fact it does the opposite, it bypasses the browser completely. This is what makes ajax so useful. Everything you echo in index.php is what jQuery receives back from the request. You need to do something with it. I'm just alerting it in my snippet as an example. I also use error to see if the request fails. You might also want to look into firebug. You can use the console tab to see all your ajax requests.

PS: You might also want to start the session in the first php script.

MrHus
I removed the $(window).unload(function () { } and left only the $.get and it worked fine. It also worked with your solution.So how can I get that script to execute only when the page is reloaded/closed?
Norbert