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:

// 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..
    // Let's generate the URL and redirect
    $url = $twitteroauth-
    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
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) {
} 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:
API updates via Twitter:
Issues/Enhancements Tracker:
Change your membership to this group:

Reply via email to