GGio
GGio

Reputation: 7653

Force redirect after Jquery AJAX Call from PHP

I have a custom framework and I detect if a request is an ajax in my Request file. In my basecontroller I check if user is logged in:

If user is not logged in:
  1. if request is ajax - force JS redirect from PHP (unable to do)
  2. if request is not ajax - PHP redirect (header, works)

I cant get number 1 to work.

Here is my redirect code:

//User::Redirect
public function redirect($url = SITE_URL, $isAjax = false) {
    if ($isAjax === true) {
        //its ajax call, echo js redirect
        echo '<script>window.location = ' . $url . ';</script>';
    } else {
        header("Location: {$url}");
    }
    exit;
}

Code for login check:

//Basecontroller::__construct
if ( ! $this->user->isLoggedIn()) {
   //redirect to login page
   $this->user->redirect('/login', $request->ajax()); 
   exit;
}

But instead of redirecting on ajax calls it just outputs <script>window.location = URL</script>

NOTE:

I know i could add check on each of my AJAX call to do redirect from there but Im trying to avoid that and let my PHP script detect in base controller and redirect rather than me adding check to all of my AJAX calls (alot).

Upvotes: 3

Views: 12389

Answers (3)

Dave
Dave

Reputation: 386

This code works for me. If an ajax call finds the user is not logged in (eg: the user has left their browser open and their session has expired) the PHP will echo out the refresh code either as JS or a meta tag to the browser to force a refresh of the whole page.

$url = '/index.php';

echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';

Upvotes: 0

Gavin
Gavin

Reputation: 2183

Your Javascript redirect needs script tags. Try this:

public function redirect($url = SITE_URL, $isAjax = false) {
    if ($isAjax === true) {
        //its ajax call, echo js redirect
        echo '<script>window.location = ' . $url . ';</script>';
    } else {
        header("Location: {$url}");
    }
    exit;
}

In terms of handling this within your ajax, assuming jQuery

$.ajax(
    {
        type: "POST",
        url: url,
        data: postParams,
        success: function(data)
        {
            //do something with success response
        },
        error : function(httpObj, txtStatus)
        {
            if(httpObj.status == 401)
            {//redirect to login
                var loginUrl = '/login';
                document.location.href = loginUrl;
            }
            else if(httpObj.status == ...)  //etc
            {//do something

            }
            else
            {
                alert("Ajax\n Error: " + txtStatus + "\n HTTP Status: " + httpObj.status);
            }
        }

Upvotes: 3

Nick
Nick

Reputation: 3156

How is the client supposed to know that is javascript?

Try encasing it with script tags i.e.

<html>
<script>
window.location = 'testpage.html'
</script>
</html>

Upvotes: 1

Related Questions