0

I use a jQuery Ajax 'get' to direct the php to the logout function. When at logout function the redirect header breaks the code and doesn't continue after that point. No Ajax response.

Cant figure out why this is happening...I have used redirects before but this one stops me continuing any code.

What am I doing wrong?

jQuery:

$(document).ready(function() 
{
    $('#logout').on('mouseup', function (e) 
    {
        logOut();
    });

    function logOut()
    {
        console.log('start');
        $.get('php/function/active-user.php?logout=true', function(response)
        {
            //This never gets called when using 'header' line in php
            console.log(response);
            console.log('end');
        });
    }
 }

PHP:

if(isset($_GET['logout']))
{   
    session_start();
    include("../data/connection.php");
    $db = connect();

    logOut($_SESSION['active-user'], $db);
}


function logOut($user_id, $db)
{
    echo 'logging-out!';
    session_destroy();
    header("Location: user-logout.php"); //header doesn't redirect and no code works after this point
    logUser('out', $user_id, $db);
    exit;

}
Orbitall
  • 611
  • 11
  • 36

2 Answers2

0
function logOut(redirectPage)
    {
        console.log('start');
        window.location = "php/function/active-user.php?logout=true";
    }

You do not need to make AJAX call here as you anyway want to redirect the User, then why just not redirect the user in the first place. I can't see any security issues here as well.

void
  • 36,090
  • 8
  • 62
  • 107
  • I need to end/destroy session, log user in database and redirect the page....this is why i have used AJAX and PHP. – Orbitall Jan 04 '16 at 12:01
  • Whatever the AJAX call is doing by hitting this URL can be done if you will redirect the User directly to this URL. AJAX is used when you want to contact the server without refreshing the page, which you anyway don't want and want to redirect your user.. – void Jan 04 '16 at 12:04
  • You saying I should just redirect the page using JS to `user-logout.php` and call logout function from that page? – Orbitall Jan 04 '16 at 12:07
  • Just redirect the user to `active-user.php?logout=true`, it will handle the logout automatically as it is already written there. – void Jan 04 '16 at 12:08
  • Thanks...I tried it out, works nice, I have got alot of same page required ajax and hadn't thought of doing it the simple way, lol. – Orbitall Jan 04 '16 at 12:54
-1

1st step would be to turn on error reporting in PHP to see the errors

for example: error_reporting(E_ALL);

Rok D.
  • 244
  • 1
  • 9