重定向后PHP会话被破坏了吗?

时间:2014-04-02 10:41:41

标签: php session yii cas

我面临一个非常奇怪的问题,我正在申请中使用CasLogin。 我已经成功实现了CAS,即在所有正确的验证之后所有值都在$ _SESSION变量中设置,并且成功登录,但是当我将它从CasLogin()操作重定向到Index Action时,$ _SESSION不包含任何内容。 我正在使用Yii Frame Work。

这是代码。

public function actionCasLogin($CID=NULL)
{
    //code to be imported from GMS here
    PhpCasControl::setPhpCasContext($CID);
    phpCAS::setPostAuthenticateCallback(array($this,'_saveServiceTkt'));
    $loginForm = new CasLoginForm;
    // validate user input and redirect to the previous page if valid

    if ($loginForm->login($CID)) {

        if (Yii::app()->user->isGuest){
            echo '<br> <br> This shows up..';
            var_dump($_SESSION);
        }
        else{
            echo 'Hello at caslogin <br>never shows up';
            var_dump(Yii::app()->user->id);
        }


        $this->redirect(array('index'));

    }
    else {
        throw new Exception("You don't have sufficient permissions to access this service. Please contact Administrator !");
    }
}

这个功能正常工作,如果我放一个EXIT; 此处将显示带有所有所需值的$ _SESSION .. 但重定向后...索引.. 其代码是这个..

public function actionIndex()
{
    echo"hello at index";

    if (! Yii::app()->user->isGuest) {
        //#CASE 1: User is already logged in

        $this->redirect(array("site/upload"));
    }
    else{
        //#CASE 2: User is not Logged in
        echo '<br>shows up with empty session<br>';
        var_dump($_SESSION);
        var_dump(Yii::getVersion());
        exit;
        $this->redirect(array("site/login"));
    }

}

此处$ _SESSION为空..

任何解释为什么会发生这种情况.. 我知道CAS通过服务票据名称创建自己的会话..我已经处理了那个东西..通过RenameSession函数,我在CasLoginForm中调用.. 其代码是这个..

public function rename_session($newSessionId) {
    //Store current session variables so that can be used later
    $old_session = $_SESSION;
    //Destroy current session
    session_destroy();
    // set up a new session, of name based on the ticket
    $session_id  = preg_replace('/[^a-zA-Z0-9\-]/', '', $newSessionId);
    //start session with session ID as 1) service ticket in case of CAS login, 2) random sTring in case of local login.
    session_id($session_id);
    session_start();

    //echo "<br>new session <br>";

    //Restore old session variables
    $_SESSION    = $old_session;
    //var_dump($_SESSION);
}

1 个答案:

答案 0 :(得分:0)

好的,我认为您应该使用session_id来更改ID。

public function rename_session($newSessionId) {
    // set up a new session id, of name based on the ticket
    session_id(preg_replace('/[^a-zA-Z0-9\-]/', '', $newSessionId));
}