Facebook PHP SDK 会话与 Yii CWebUser 冲突
我目前正在使用 Yii 框架开发一个 Facebook 画布应用程序,它有多个选项卡。
设置应用的 CWebUser 组件时出现问题。
我正在 SiteController
的 actionIndex()
方法中设置 user
组件。
设置后,我想重定向到我的应用程序中的特定页面选项卡。
在重定向的页面中,我想使用用户信息,但用户组件信息丢失了。
SiteController 的代码如下:
<?php
class SiteController extends Controller{
// other functions
public function actionIndex(){
// call the Facebook PHP SDK to getUser();
$fbHelper = new FacebookHelper();
$userInfo = $fbHelper->getCurrentInfo();
Yii::app()->user->setId($value['id']);
Yii::app()->user->setName($value['name']);
// further i set some more values that are required by the app using setState()
// query the db to find out what the user was doing last time
// and then redirect accordingly to the next page
$this->redirect(array('pagelist/index'));
}
}
新页面的代码:
<?php
class PagelistController extends Controller{
public function actionIndex() {
$this->render('index');
}
}
PagelistController 的 index.php 视图中的代码:
<?php
echo "Hi ".Yii::app()->user->name;
// Yii::app()->user->getIsGuest(); returns true, which shouldn't be the case.
显然,用户信息在重定向中丢失了。
现在我收集到的是,Facebook 的 PHP SDK 正在使用会话来存储其实例,CWebUser 组件也是如此。所以我的猜测是 FB PHP SDK 正在清除重定向上的会话,这最终也会清除 Yii 应用程序会话。或者类似的事情正在发生!!
那么我们如何在应用程序的生命周期中保留用户信息呢?
我应该修改 FB PHP SDK,以便它使用 yii 应用程序已生成的会话吗?或者也许阻止它清除重定向时的会话?
感谢任何帮助。
编辑:
我将会话与以下代码一起使用,包含在 main.php 配置文件中,作为应用程序组件。但是,如果删除此代码,一切都会正常工作,有什么想法可以解释为什么这会阻止会话保留吗?
'session'=>array(
'sessionName'=>'PageType',
'autoStart'=>true,
'cookieMode'=>allow,
'class' => 'system.web.CDbHttpSession',
'connectionID' => 'db',
'sessionTableName' => 'tbl_session',
),
I am currently developing a Facebook canvas app, which has multiple tabs, using the Yii framework.
And there is a problem in setting the CWebUser component for the app.
I'm setting the user
component in SiteController
's actionIndex()
method.
After setting that, i want to redirect to a particular page tab within my app.
In the redirected page, i want to use the user info, but the user component info is lost.
The code for the SiteController is below:
<?php
class SiteController extends Controller{
// other functions
public function actionIndex(){
// call the Facebook PHP SDK to getUser();
$fbHelper = new FacebookHelper();
$userInfo = $fbHelper->getCurrentInfo();
Yii::app()->user->setId($value['id']);
Yii::app()->user->setName($value['name']);
// further i set some more values that are required by the app using setState()
// query the db to find out what the user was doing last time
// and then redirect accordingly to the next page
$this->redirect(array('pagelist/index'));
}
}
The code for the new page:
<?php
class PagelistController extends Controller{
public function actionIndex() {
$this->render('index');
}
}
The code in the index.php view for PagelistController:
<?php
echo "Hi ".Yii::app()->user->name;
// Yii::app()->user->getIsGuest(); returns true, which shouldn't be the case.
So obviously the user info is getting lost in the redirection.
Now what i gather is, that Facebook's PHP SDK is using sessions to store its instance, and so does the CWebUser component. So my guess is that the FB PHP SDK is clearing the session on redirects, which also ends up clearing the Yii app session. Or something close to that is happening!!
So then how do we persist the user info during the application's lifetime?
Should i be modifying the FB PHP SDK, so that it uses the already generated session of the yii app? Or maybe prevent it from clearing the session on redirects?
Appreciate any help.
Edit :
I was using sessions with the code below, included in the main.php configuration file, as an application component. However if this code is removed everything works fine, any ideas as to why this prevents the session retention?
'session'=>array(
'sessionName'=>'PageType',
'autoStart'=>true,
'cookieMode'=>allow,
'class' => 'system.web.CDbHttpSession',
'connectionID' => 'db',
'sessionTableName' => 'tbl_session',
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在使用为 facebook 连接创建的以下扩展...
https://github.com/ielshareef/FacebookExtension4Yii
我发现它很有用,直到现在都没有问题。即使我已经更新了最新版本的 PHP-SDK 并根据我的性质做了一些更改。
此扩展使用 userIdentity 接口来设置当前登录的在 Facebook 上默认 CWebUser ...
I'm using the following extension which is created for facebook connect...
https://github.com/ielshareef/FacebookExtension4Yii
I found it useful and until now there is no problem.. Even i've updated the latest version of PHP-SDK and made some changes according to my nature..
This extension uses the userIdentity interface to set the current logged in facebook to the default CWebUser ...
尝试使用 CWebUser::setState() 来更新会话Yii::app()->user->setName,例如:
Try using CWebUser::setState() to update the session instead of Yii::app()->user->setName, e.g.:
为了避免 facebook API 会话和 Yii 会话之间发生冲突,您应该编辑文件 facebook.php 并在构造函数中替换
To avoid conflict between facebook API session and Yii session you should edit the file facebook.php and, inside the constructor, replace
by