【问题标题】:Yii loses session after OAuth redirectYii 在 OAuth 重定向后丢失会话
【发布时间】:2015-05-27 18:49:17
【问题描述】:

在我转到 Twitter 的 OAuth 页面并返回重定向后,我花了一天半的时间试图弄清楚为什么 Yii 会删除所有会话数据。

这是主要的 SiteController,我在其中访问 Twitter。在这里,我尝试保存 oauth_token 和 token_secret 值,以便可以在重定向控制器上使用它们。

function actionTwitter()
{
    $consumerKey = ""; 
    $consumerSecret = "";

    $connection = new TwitterOAuth($consumerKey, $consumerSecret); 

    $request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://127.0.0.1/yii/?r=redirect&type=twitter"));

    $oauth_token=$request_token['oauth_token'];
    $token_secret=$request_token['oauth_token_secret'];

    Yii::app()->session['token'] = $oauth_token; // This doesn't save!!
    Yii::app()->session['token_secret'] = $token_secret; // This does not save!!

    $url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));

    $this->redirect($url);

    exit(); // some people have said I need to exit the session first after I redirect, but it doesn't help at all.
}

这是我的 RedirectController,它是一个单独的控制器,不在主 SiteController 中:

public function actionIndex()
{
    $type = $_GET['type']; 

    if ($type == "twitter")
    {
        $token = Yii::app()->session['token'];
        print($token);
    }
 }

我还在我的配置文件中将会话自动启动设置为 true。

关于为什么它不起作用/我读过的东西的想法:

  • Twitter 的站点是 HTTPS,我在 localhost(不是 HTTPS)上。出于某种原因,我忘记了这会使我重定向时会话丢失数据。如果是这种情况,如何在不使用 HTTPS 的情况下修复它?
  • 当我创建新的 CHttpCookies 时,它们也不保存,我无法检索值
  • 我已经尝试过 Yii::app()->user->setState ,但也不起作用。

【问题讨论】:

    标签: php session yii oauth


    【解决方案1】:

    我找到了解决方案。它不起作用,因为我使用 127.0.0.1 进行重定向,而不是标准的 localhost。我改变了它,现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-31
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      相关资源
      最近更新 更多