简体   繁体   English

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

[英]PHP Session gets destroyed after redirection?

I am facing a very strange problem , i am doing CasLogin in my application.. i have successfully implemented CAS, ie all values are set in $_SESSION variable after all proper validations, and successful login, but when i redirect it from CasLogin() action to Index Action $_SESSION contains nothing.. i am using Yii Frame Work. 我面临一个非常奇怪的问题,我正在我的应用程序中执行CasLogin ..我已经成功实现了CAS,即在所有正确验证和成功登录后,所有值都在$ _SESSION变量中设置,但是当我从CasLogin()重定向它时动作到索引动作$ _SESSION不包含任何内容。我正在使用Yii框架。

here is code. 这是代码。

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 !");
    }
}

this function Works Properly and if i put a EXIT; 此功能正常工作,如果我放出口; here it will display $_SESSION with all the desired values.. but after redirection... to index.. whose code is this.. 在这里它将显示$ _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"));
    }

}

here $_SESSION is empty.. $ _SESSION在这里是空的。

any explanation why this might be happening.. i am aware of CAS creating its own session by service ticket name.. i have handled that thing.. by RenameSession function, which i call in CasLoginForm.. whose code is this.. 任何解释为什么可能会发生这种情况..我知道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);
}

OK, i think that you should use session_id to change the id. 好的,我认为您应该使用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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM