title: Force redirect after Jquery AJAX Call from PHP
body: <p>I have a custom framework and I detect if a request is an <strong>ajax</strong> in my <strong>Request</strong> file. In my basecontroller I check if user is logged in:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I cant get number <strong>1</strong> to work. </p>
  
  <p>Here is my redirect code:</p>
  
  <pre><code>//User::Redirect
  public function redirect($url = SITE_URL, $isAjax = false) {
      if ($isAjax === true) {
          //its ajax call, echo js redirect
          echo '&lt;script&gt;window.location = ' . $url . ';&lt;/script&gt;';
      } else {
          header("Location: {$url}");
      }
      exit;
  }
  </code></pre>
  
  <p>Code for login check:</p>
  
  <pre><code>//Basecontroller::__construct
  if ( ! $this-&gt;user-&gt;isLoggedIn()) {
     //redirect to login page
     $this-&gt;user-&gt;redirect('/login', $request-&gt;ajax()); 
     exit;
  }
  </code></pre>
  
  <p>But instead of redirecting on <strong>ajax</strong> calls it just outputs <code>&lt;script&gt;window.location = URL&lt;/script&gt;</code></p>
  
  <p><strong>NOTE:</strong></p>
  
  <p>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).</p>