Authentication works sometimes and sometimes it fails


Goal: I have a login button on my website. When a user presses the button, he/she authorizes the application and a post is made on their twitter account.
Setup: I have created a new application, the permissions are set to read/write and the check box: application allows sign in (similar) is checked as well.

I am referring to two tutorials:


Current Situation

  1. The user enters some content and user presses the button, he/she is sent to twitter side and authorized.
  2. The user is forwarded back to my website when the post is made - this is not consistent.

Currently, I am getting stuck at this piece of code:
if (! empty($_GET[‘oauth_verifier’]) && ! empty($_SESSION[‘oauth_token’]) && ! empty($_SESSION[‘oauth_token_secret’])) {
} else { // some params missed, back to login page
$TwitterOauth = new TwitterOAuth($sConsumerKey, $sConsumerSecret, $_SESSION[‘oauth_token’], $_SESSION[‘oauth_token_secret’]);
$AccessToken = $TwitterOauth->getAccessToken($_GET[‘oauth_verifier’]); // get access tokens
$User = $TwitterOauth->get(‘account/verify_credentials’); // get account details
echo $User[‘id’];
$_SESSION[‘oauth_token’] = $access_token[‘oauth_token’];
$_SESSION[‘oauth_token_secret’] = $access_token[‘oauth_token_secret’];
// header(“Location:”);

The “echo” statement is for debugging purpose. Currently, I am not getting any id. If I also do print_r($_SESSION), then no values are printed.
However, sometimes (with echo and print statements commented out) I get redirected to post.php and the post is tweeted. Sometimes, I get stuck at the page which has above piece of code. I do not receive any error message. I just see a blank page.

Can someone please help me?? Please??