0
votes

I am facing a very strange problem , i am doing CasLogin in my application.. i have successfully implemented CAS, i.e 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.

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..

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..

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..

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
Why do you need to change the session ID? - Dinistro
Yii makes own session, and CAS makes a session with service ticket ID..so to make both point to same session. i have to change the id. - Himanshu97
But i still can't understan, why you need to reset the session_id, can't you just add your cas-id as a session value? - Dinistro
i think i cudnt explain myself well enough, see thing is : CAS creates a session by itself, with session id same as serviceticket, and my application creates a session with some other sessionid.. Yii::app()->session... what i want both sessions shud point to same space, so my application's workflow dont change @Dinistro - Himanshu97
You can not have multiple Sessions in 1 Application. Does the CAS contains session_start()? Can you please add a var_dump() of the CAS Session and the normal Session. - Dinistro

1 Answers

0
votes

OK, i think that you should use session_id to change the 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));
}