开发者

facebook session cookie problem

开发者 https://www.devze.com 2023-03-03 16:55 出处:网络
I\'m using the php sdk on my canvas app in facebook. i have 3 pages: index.php play.php & register.php

I'm using the php sdk on my canvas app in facebook. i have 3 pages: index.php play.php & register.php

in both index & play i check if there is a session in this manner:

$session = $facebook->开发者_如何学JAVA;getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl(array('canvas' => 1,
                                          'fbconnect' => 0,
                                          'req_perms' => 'publish_stream,email',
                                          'next' => 'register.php',
                                          'cancel_url' => 'http://www.facebook.com/apps/application.php?id=MYAPPID&perms' ));
}

if (!$me){ print("<html><head>");
print("<script type=\"text/javascript\">function redirect(){ top.location.href = \"");
print($loginUrl);
print("\"; }</script></head>");
print("<body onload=\"redirect();\">Please wait...</body></html>"); exit(); }

So that if i don't know who the user is and his new to my app, direct him to register.php and put his uid in my DB.

The problem i have is that some users can see correctly the entry page index.php but when navigating to play.php they get redirected to the register page. After that the same thing happens every time they try to enter the play page.

Something is terribly wrong here. i've tried clearing cookies but nothing seem to work.


I'd probably think that $facebook->api(...) can return values that are negative positives (ie. returns a value that evaluates to TRUE, but is really not, as in a string 'UNAUTHORIZED', or an empty array). Relying on just the not operator ('!') is probably not all that reliable, and I would check for return types (although I don't know the API and can't tell you what it is)

Best thing to do is a var_dump ( $facebook->api('/me') ) ; to see what it actually returns. Also you could try a proper test instead of just the not operator and the imperative, so instead of ;

if ( $me ) { ... }

Try ;

if ( is_object ( $me ) ) { ... }
if ( is_array ( $me ) ) { ... }
if ( ! empty ( $me ) ) { ... }

Or any of the other things that you could test for through $facebook->api('/me'), or even something specific like ;

if ( $me !=== NULL ) { ... }

Anyway, I'm sure your problem lies within the business logic of how to test for your return data.

0

精彩评论

暂无评论...
验证码 换一张
取 消