使用Yii在两个项目中打开会话

时间:2013-11-25 07:58:08

标签: php session cookies login yii

我处境很复杂。我有两个项目。一个是使用Yii Framework设计的,另一个只包含纯PHP代码(我的意思是没有框架)。我想要做的是,当用户登录项目时,他/她应该登录Yii项目。我试图在Yii项目中设置session_id,但它没有用。我通过使用php头函数重定向用户以便Yii项目以便让用户登录。以下是代码:

PHP项目:

    if (isset($_POST['giris-yap'])) {

        $_POST['eposta']    = $this->cstring->validEmail($_POST['eposta']);
        $_POST['sifre']     = md5($_POST['sifre']);

        if ($_POST['eposta'] != '' && $_POST['sifre'] != '') {
            $params = array(
                'e-posta' => $_POST['eposta'],
                'sifre' => $_POST['sifre']
            );
            $sonuc = $this->loginKendim($params);

            if ($sonuc) {
                 $_SESSION['_utmkendim'] = md5('üyegirişyaptı'.$_POST['eposta']);

                //mya user authentication
                if($_SERVER["REMOTE_ADDR"] = "88.248.192.175")
                {
                    header("Location: https://mya.genel.com/?sessionId=" . $_COOKIE["PHPSESSID"]);
                    exit;
                }
                //end of mya user authentication

                header("Location: https://www.kendim.com/panel");
                exit;
            }
            else
                $this->_kayithata = 1;
        }
        else
            $this->_kayithata = 1;
    }  

Yii项目:

public function beforeAction($action) {

    if(isset($_GET["sessionId"]) && $_GET["sessionId"] != "")
    {
        Yii::app()->session->setSessionID($_GET["sessionId"]);
        session_id($_GET["sessionId"]);

        header("Location: https://www.kendim.com/panel");
        exit;
    }

    # mya wl-api den gelen kullanıcıyı login edelim.
    if(isset($_GET['_MYAU'])){
        $_useruniqueid = Yii::app()->CString->CleanSTR($_GET['_MYAU']); 
        $userlogin = UserslogLogin::model()->findByAttributes(array('login_uniqueid' => $_useruniqueid ));
        if($userlogin){

            $user = UsersAccounts::model()->findByPk($userlogin->user_id, array('select' => 'user_id, user_name, user_email, user_pass'));
            $identity = new UserIdentity($user->user_email, $user->user_pass);
            $identity->id = $user->user_id;
            $identity->name = $user->user_name;
            $identity->username = $user->user_email;

            $duration = 3600*24; //$this->rememberMe ? 3600*24*30 : 1800; // 30 days

            Yii::app()->user->login($identity, $duration);

            Yii::app()->request->redirect('/anasayfa');
            Yii::app()->end();

        }else{
            $action = 'actionError';

            $this->$action();
            Yii::app()->end();             
        }

   }


   # kullanıcı login değilse indexe yönlendir
    if(Yii::app()->user->isGuest){
        $action = 'actionIndex';
        $this->$action();
        Yii::app()->end();   
    }else{

        $this->getuser = UsersAccounts::user();
         Controller::$projectModel =  Projects::loadModel($this->getuser->project_code);
    }


    return parent::beforeAction($action);
}

2 个答案:

答案 0 :(得分:1)

我不认为问题是框架。问题是域名,正如DaSourcerer所猜测的那样。 看看上一篇文章:

Preserving session variables across different domains

答案 1 :(得分:0)

好的,我找到了解决方案。我忘了使用session_start();.问题解决了。我现在可以在域名之间转移会话。