Facebook 会话 cookie 问题
我在 facebook 的画布应用程序上使用 php sdk。 我有 3 个页面:index.php play.php & register.php
索引和目录中的 我以这种方式检查是否存在会话:
$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);
}
}
// 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(); }
这样,如果我不知道用户是谁以及他是我的应用程序的新用户,请引导他访问 register.php 并将他的 uid 放入我的数据库中。
我遇到的问题是,一些用户可以正确看到入口页面index.php,但当导航到play.php时,他们会被重定向到注册页面。此后,每次他们尝试进入播放页面时都会发生同样的情况。
这里出了严重的问题。我尝试过清除cookie,但似乎没有效果。
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->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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能认为
$facebook->api(...)
可以返回负正值(即返回一个计算结果为 TRUE 的值,但实际上不是,如字符串“UNAUTHORIZED”或空数组)。仅依赖非运算符('!')可能不太可靠,我会检查返回类型(尽管我不知道 API 并且无法告诉你它是什么)最好的办法是
var_dump ( $facebook->api('/me') ) ;
查看它实际返回的内容。您也可以尝试进行适当的测试,而不仅仅是 not 运算符和命令式,因此而不是 ;尝试 ;
或者您可以通过 $facebook->api('/me') 测试的任何其他内容,甚至是特定的内容,例如 ;
无论如何,我确信您的问题在于如何测试返回数据的业务逻辑。
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 ;Try ;
Or any of the other things that you could test for through $facebook->api('/me'), or even something specific like ;
Anyway, I'm sure your problem lies within the business logic of how to test for your return data.