Rsmithy
Rsmithy

Reputation: 312

ajax logout redirect

public static function logout(){
        DB::query("DELETE FROM webchat_users WHERE name = '".DB::esc($_SESSION['user']['name'])."'");


        $_SESSION = array();
        unset($_SESSION);

        return array('status' => 1);
        window.location.replace("http://domain.com/index.php");

    }

This is the code I use to logout of a chat window, that is ran using AJAX. I'm just wondering if there is a way so it will do a redirect when the button is pressed. This is the procedure that is ran on.click. I've currently experimented with window.location. but that doesn't seem to do the trick.

How would I do this?

Upvotes: 0

Views: 1458

Answers (1)

Tchoupi
Tchoupi

Reputation: 14681

If it's an Ajax call you cannot redirect the user. If you use header('Location: ...') you will redirect the Ajax requestion, which will have no effect on the user.

You can either:

  1. redirect the user to the logout page, instead of sending an Ajax request and use header('Location: ...') to send him back to your home page
  2. Use an ajax call, but the window.location must be done in the javascript par after the ajax call. In the onSuccess event for instance

Upvotes: 1

Related Questions