0

How can I get function is_twitter_logged_in() to recognize variable $Twitter that's assigned in the previous function?

function twitter_logged_in($twitter_user) 
{
global $consumer_key; 
global $consumer_secret; 

$Twitter = new EpiTwitter($consumer_key, $consumer_secret);

if(isset($_GET['oauth_token']) || (isset($_COOKIE['oauth_token']) && isset($_COOKIE['oauth_token_secret'])))
{
  // user has signed in
  if( !isset($_COOKIE['oauth_token']) || !isset($_COOKIE['oauth_token_secret']) )
{
    // user comes from twitter
            // send token to twitter
    $Twitter->setToken($_GET['oauth_token']);
           // get secret token
    $token = $Twitter->getAccessToken();
            // make the cookies for tokens
    setcookie('oauth_token', $token->oauth_token);
    setcookie('oauth_token_secret', $token->oauth_token_secret);
           // pass tokens to EpiTwitter object
    $Twitter->setToken($token->oauth_token, $token->oauth_token_secret);

}
else
{
 // user switched pages and came back or got here directly, stilled logged in
    // pass tokens to EpiTwitter object

 $Twitter->setToken($_COOKIE['oauth_token'],$_COOKIE['oauth_token_secret']);
    }
}
elseif (isset($_GET['denied'])) {
 // user denied access
 echo 'You must sign in through twitter first';
}
else {
// user not logged in
     echo 'You are not logged in';
}
global $Twitter;

$twitter_user= $Twitter->get_accountVerify_credentials();
// show screen name (not real name)
echo $twitter_user->screen_name;
// show profile image url
//$twitter_image = $user->profile_image_url;
return $Twitter;
}

function is_twitter_logged_in($Twitter) {
global $Twitter;
$twitter_user = $Twitter;
if ( $twitter_user->screen_name == '' ){return false;}
    else {return true;}
}
3
  • The question is not very clear. There is no $Twitter variable in twitter_connect_mod() so what is the 'previous' function? Spend some time for us. And please, indent your code or pastebin it indented elsewhere Commented May 3, 2011 at 16:46
  • twitter_connect_mod() is not really relevant here, its the last two functions that I'm trying to fix. Commented May 3, 2011 at 17:07
  • So tailor the test case, please, so that it is smallest but still self-contained and compilable. Commented May 3, 2011 at 17:09

1 Answer 1

4

$Twitter must first be defined outside of the function scope, then it can be referenced in functions by using global $Twitter and is_twitter_logged_in() doesn't even use $Twitter

Sign up to request clarification or add additional context in comments.

9 Comments

True $Twitter isn't in is_twitter_logged_in(), but $Twitter doesn't need to be first defined outside function scope - it can be defined from inside a function with global $Twitter
I am doing that .. but its still returning an error: Trying to get property of non-object
Or alternatively, you can access it as $GLOBALS['Twitter'] . The variable $GLOBALS is itself a super global, and is an array with all 'globals' inside.
+1 But please avoid the usage of the global keyword unless its really necessary to use some legacy code. global is messy. It makes debug and code reading less easy.
Are you saying that defining a variable $GLOBALS['Twitter'] inside the first function will allow me to reuse the value that was assigned to that variable now in the second function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.