title: PHP log out with AJAX call
body: <p>I have a logout function that looks like this.</p>
  
  <blockquote>
  <pre><code>if ($_GET["argument"]=='logOut'){
      if(session_id() == '') {
          session_start();
      }
      session_unset();
      session_destroy();
      $host  = $_SERVER['HTTP_HOST'];
      $extra = 'index.php';
      header("Location: http://$host/$extra");
      exit; 
  }
  </code></pre>
  </blockquote>
  
  <p>My problem is that, If I inspect the page and look at the <code>Network</code> preview and response, it looks 'fine'. </p>
  
  <p>There are two php files listed that was processed.</p>
  
  <p><code>http://localhost:5000/inc/mainScripts.php?argument=logOut</code></p>
  
  <p>which is where the function is located.</p>
  
  <p>and <code>http://localhost:5000/index.php</code></p>
  
  <p>Which is where i would like to be redirected.</p>
  
  <p>The <code>Response</code> tab in the <code>Network</code> of the inspect page area in chrome
  contains the full login html page <code>login.php</code> but in the browser it remains in the same place. Like the <code>header</code> command has never been called.</p>
  
  <p>What Am I doing wrong? </p>
  
  <p><strong>HTML AJAX call to this function:</strong></p>
  
  <blockquote>
  <pre><code>        $("#logout_btn").click(function() {
              $.ajax({
                  url: './inc/mainScripts.php?argument=logOut'
              })
          });
  </code></pre>
  </blockquote>
  
  <hr>
  
  <h2><strong>SOLUTION</strong></h2>
  
  <p><strong><em>AJAX</em></strong></p>
  
  <blockquote>
  <pre><code>        $("#logout_btn").click(function() {
              $.ajax({
                  url: './inc/mainScripts.php?argument=logOut',
                  success: function(data){
                      window.location.href = data;
                  }
              });
          });
  </code></pre>
  </blockquote>
  
  <p><strong><em>PHP</em></strong></p>
  
  <blockquote>
  <pre><code>if ($_GET["argument"]=='logOut'){
      if(session_id() == '') {
          session_start();
      }
      session_unset();
      session_destroy();
      $host  = $_SERVER['HTTP_HOST'];
      $link = "http://$host/index.php";
      echo $link; 
  }
  </code></pre>
  </blockquote>