$ facebook-> getSession()在示例代码中返回null。这可以吗? [英] $facebook->getSession() returns null in the example code. is that ok?

查看:99
本文介绍了$ facebook-> getSession()在示例代码中返回null。这可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 Facebook API示例代码我得到一个空会话对象,我应该得到一个非空对象给代码中的注释。



换句话说,在我的index.php中,示例代码中的这个片段显示了没有会话当我在浏览器中浏览 http://apps.facebook.com/my_app 时:

 <?php 

require'./facebook.php';

//创建我们的Application实例。
$ facebook = new Facebook(array(
'appId'=>'...',//这个帖子的...替换的实际值
'secret'= >'...',//实际值替换为'...'作为帖子
'cookie'=> true,
));

//根据$ _GET或$ _COOKIE的会话,我们可能有也可能没有这个数据。
//
//如果我们在这里获得一个会话,这意味着我们使用
//应用程序秘密只有Facebook和应用程序知道了一个正确签名的会话。我们不知道
//如果它仍然有效,直到我们使用会话进行API调用。会话
//如果已经过期(在这种情况下不应该得到
//会话),或者用户从Facebook注销,则可能无效。
$ session = $ facebook-> getSession();

if($ session){
echosession ok;
}
else {
echono session;
}


?>

注意:在我的服务器index.php和facebook.php在同一个文件夹。

解决方案$ c> if($ session){
$ logoutUrl = $ facebook-> getLogoutUrl();
} else {
$ loginUrl = $ facebook-> getLoginUrl();
}
<?php if($ me):?>
< a href =<?php echo $ logoutUrl;?>>
< img src =http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif>
< / a>
<?php else:?>
< div>
使用JavaScript& amp; amp; amp; XFBML:< fb:login-button>< / fb:login-button>
< / div>
< div>
不使用JavaScript& amp; amp; amp; XFBML:
< a href =<?php echo $ loginUrl;?>>
< img src =http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif>
< / a>
< / div>
<?php endif?>

从您提供的链接中获取的代码。


Running the example code for the Facebook API I get a null session object, and I should get a non-null object giving the comment in the code. What am I doing wrong?

In other words, in my index.php this fragment from the example code shows "no session" when I go to http://apps.facebook.com/my_app in my browser:

<?php

require './facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => '...', // actual value replaced by '...' for this post
  'secret' => '...', // actual value replaced by '...' for the post
  'cookie' => true,
));

// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();

if ($session) {
  echo "session ok";
}
else {
  echo "no session";
}


?>

Note: in my server index.php and facebook.php are in the same folder.

解决方案

Have you linked your app yet?

if ($session) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}
 <?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript &amp; XFBML:
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
</div>
<?php endif ?>

Code taken from the link you provided.

这篇关于$ facebook-&gt; getSession()在示例代码中返回null。这可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆