Is it possible to check to see if a user is logged in to Twitter and
has authorized the application without having any redirects?

For first time login, I have this basic code:
<?
require("twitter/twitteroauth.php");
session_start();

// The TwitterOAuth instance
$twitteroauth = new TwitterOAuth('xxx', 'xxx');
// Requesting authentication tokens, the parameter is the URL we will
be redirected to
$request_token = $twitteroauth->getRequestToken('xxx');

// Saving them into the session
$_SESSION['twitter_token'] = $request_token['oauth_token'];
$_SESSION['twitter_secret'] = $request_token['oauth_token_secret'];

// If everything goes well..
if($twitteroauth->http_code==200){
    // Let's generate the URL and redirect
    $url = $twitteroauth-
>getAuthorizeURL($request_token['oauth_token']);
    header('Location: '. $url);
}
?>

That's on a separate page, something like "twitter-login.php," so I
only want to use that when the user isn't logged in (i.e. the user
clicks a "login with Twitter" link).

I have a function for my site's user system that displays either a
login form (if not logged in; has Facebook/Twitter buttons) or a user
control panel (if logged in). The function prototype is something
like:
displayLogin(bool $facebook, bool $twitter, string $username)
where the first two parameters indicate if the user is signed in with
Facebook, Twitter, or neither.

With Facebook, I can check with the API if the user has a session on
Facebook and has verified the application by:
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
        error_log($e);
  }
} else {
        $me = false;
}

if ($me) {
   // do stuff because the user is logged in and has authorized the
app in the past
}

And so if $me isn't false, I know the user is logged in and I can set
session variables, and pass a boolean true to the displayLogin()
function for Facebook. All of this is processed on my index page,
hence no redirects.

Is there any way to do this with Twitter without having to do the
redirect? Would I have to use javascript or something? My PHP library
is the twitteroauth library.

Thank you!

-- 
Twitter developer documentation and resources: http://dev.twitter.com/doc
API updates via Twitter: http://twitter.com/twitterapi
Issues/Enhancements Tracker: http://code.google.com/p/twitter-api/issues/list
Change your membership to this group: 
http://groups.google.com/group/twitter-development-talk

Reply via email to