title: Twitter login on button click using abraham's oauth
body: <p>I saw previous similar threads but none of them solved my issue.<br>
  I have put button on my site and want to perform user twitter login there.</p>
  
  <p>When I execute php script directly it gives expected result, but while calling it from html button click, it gives "302 found error" in firebug console.</p>
  
  <p>What is wrong here?</p>
  
  <p>I want to get          <code>echo json_encode((object) array('id' =&gt; $id, 'name' =&gt; $name));</code>
  in login.php as a json result.</p>
  
  <p>HTML/JS code</p>
  
  <pre><code>&lt;input type = "button" id = "loginTwitter" class = "btn btn-primary"  value = "Login | Twitter "/&gt;
  function getTwitterVal(clb)
  {
      var xmlhttp;
      if (window.XMLHttpRequest)
      {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      }
      else
      {
          // code for IE6, IE5
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
      xmlhttp.onreadystatechange=function()
      {
                  var result = xmlhttp.responseText;
                          alert(result);
  //                var obj = JSON.parse(result);
                  clb(obj);
      }
      xmlhttp.open("POST","p3.php",true);
      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.send();
      // api_call();
  }
  $('#loginTwitter').click(function data() {
      getTwitterVal(function(obj) {
  //        alert(obj.name);
      });
  });
  </code></pre>
  
  <p>p3.php</p>
  
  <pre><code>&lt;?php
  /* Load required lib files. */
          session_start();
          require_once('twitteroauth/twitteroauth.php');
          require_once('config.php');
          $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
          $request_token = $connection-&gt;getRequestToken(OAUTH_CALLBACK);
          $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
          $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
          switch ($connection-&gt;http_code)
          {
          case 200:
                  $url = $connection-&gt;getAuthorizeURL($token);
                  header('Location: ' . $url);
                  break;
          default:
                  echo 'Could not connect to Twitter. Refresh the page or try again later.';
          }
  ?&gt;
  </code></pre>
  
  <p>callback.php</p>
  
  <pre><code>&lt;?php
  session_start();
  require_once('twitteroauth/twitteroauth.php');
  require_once('config.php');
  if (isset($_REQUEST['oauth_token']) &amp;&amp; $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
    $_SESSION['oauth_status'] = 'oldtoken';
    header('Location: ./clearsessions.php');
  }
  $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
  $access_token = $connection-&gt;getAccessToken($_REQUEST['oauth_verifier']);     
  $_SESSION['access_token'] = $access_token;     
  unset($_SESSION['oauth_token']);
  unset($_SESSION['oauth_token_secret']);
  if (200 == $connection-&gt;http_code) {
    $_SESSION['status'] = 'verified';
    header('Location: ./index.php');
  } else {
    header('Location: ./clearsessions.php');
  }
  </code></pre>
  
  <p>index.php</p>
  
  <pre><code> &lt;?php
              session_start();
              require_once('twitteroauth/twitteroauth.php');
             require_once('config.php');
              if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
                  header('Location: ./clearsessions.php');
              }
              $access_token = $_SESSION['access_token'];
              $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
              $content = $connection-&gt;get('account/verify_credentials');
             $id = $content-&gt;{'id'};
             $name = $content-&gt;{'name'};
              echo json_encode((object) array('id' =&gt; $id, 'name' =&gt; $name));
      ?&gt;
  </code></pre>