我正面臨一個非常奇怪的問題,我正在做我的應用程序中的CasLogin .. 我已經成功實施CAS,即所有值都設置$ _SESSION變量經過所有適當的驗證,併成功登錄,但是當我重定向它從CasLogin()動作索引動作$ _SESSION不包含任何內容。 我正在使用Yii Frame Work。重定向後PHP會話被破壞?
這裏是代碼。
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);
}
爲什麼您需要更改會話ID? – Dinistro
Yii創建自己的會話,並且CAS與服務票證ID進行會話,以使兩者指向同一個會話。我必須改變身份證。 – Himanshu97
但我仍然無法解釋,爲什麼您需要重置session_id,難道您不能只將您的cas-id添加爲會話值嗎? – Dinistro