Php函数变量传递

时间:2011-05-03 16:41:39

标签: php function variables argument-passing

如何获取函数is_twitter_logged_in()来识别在上一个函数中分配的变量$ Twitter?

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;}
}

1 个答案:

答案 0 :(得分:4)

首先必须在函数范围之外定义

$Twitter,然后使用global $Twitter在函数中引用它,is_twitter_logged_in()甚至不使用$Twitter