PHP killSession () function inside $ (window) .unload

I am doing a simple chat script and I have a killSession function that kills the current session and removes the user from the DB. The problem is that after the name is set and validated, the chat form doesn't load, it just kills the session and falls back to 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 call killSession () only after refreshing or closing the page?

UPDATE:

I 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']);

      

$ .get still doesn't work, so I tried it through the browser. killSession.php? name = test will remove from the database, but will not kill the session.

+2


source to share


3 answers


The session is destroyed immediately when the PHP function is called killSession

. This way your code will kill the session whenever it is executed.

What you need to do is call the PHP function killSession

only when the event is fired onunload

. And calling a PHP function from JavaScript is only possible when a new request is sent to the server.

So killSession

what you need to do is send a request to a script that then calls a function , perhaps something like this:



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

      

And inside killSession.php you are calling the PHP function killSession

.

+5


source


The request doesn't work because it is asynchronous, which means you don't need javascript to wait on the server. Executing the request synchronously will force javascript to 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");
            }
        });
    });
});

      

In ajax:



I assume that you think that whenever you call "Location": index.php in php (per ajax). That the browser will redirect there. Ajax doesn't work that way, in fact it does the opposite, it bypasses the browser entirely. This makes Ajax so useful. All you echo in index.php is what jQuery gets back from the request. You need to do something about it. As an example, I am just warning about this in my snippet. I am also using a bug to check if the request failed. You can also take a look at firebug . You can use the console tab to view all your ajax requests.

PS: You can also start a session in the first php script.

+1


source


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

0


source







All Articles