2013-07-10 117 views
0

我試圖通過會話登錄到CakePHP應用程序credentials從外部自定義PHP代碼創建。但我無法弄清楚如何完全做到這一點。我不想用形式去做。cakephp身份驗證通過會話登錄

例如: 我在網站A登錄,我的CakePHP應用程序是網站B(都託管在同一臺服務器上,但位於不同的文件夾中)。

我已經從檢索據點A username/password套入B.

但我不知道現在如何在網站上使用相同B.這

任何想法?

謝謝。

注: 好吧,我接受在CakePHP的會話的用戶名 即$this->Session->read('Username');

現在想通過驗證 如$this->Auth->login($this->Session->read('Username')); 所以,我可以再通過$這個 - > Auth->用戶獲取用戶(登陸);

你能幫我嗎? 我在AppController中執行此操作,方法如下:beforeFilter(),_checksession()

+0

代碼示例將解釋一眼就可以添加幾個。 –

+0

@Arun_C_C我現在試着在註釋部分解釋以上內容。 –

回答

0

如果您已經擁有從站點A發送的用戶名和密碼,難道您只是再次驗證數據庫的憑據嗎?或者創建一個存儲會話表,並將其鏈接到用戶名,只是把這個會話變量通過PHP $ _SESSION變量

站點A:

session_start(); 
$this->Session->write('session','ABCDEFGHIJKL12345'); // random generated string 
// store in DB 

,然後當你到網站B:

SITE B:

session_start(); 
// if $this->Session->read('session') == '' force login again 
//else 

//compare the session in the db and check for valid session 

//if valid 

//load page. 
0

如果你已經在會話的用戶名/密碼,就像你說的,嘗試建立用戶模型數據陣列驗證將嘗試自動驗證你:

// Construct model data array like what AuthComponent would usually expect 
$userData = array(
    'User' => array(
     'username' => $this->Session->read('username'), // Use your stored values (change as appropriate) 
     'password' => $this->Session->read('password') 
    ) 
); 

// Attempt to authenticate the user 
if ($this->Auth->login($userData)) { 

    // Login worked 

} else { 

    // Login failed 

} 
0

感謝所有, 我得到了一個解決方案,而不是發送密碼,只送上我的用戶名和適當的驗證後,我嘗試爲登錄:以前

$username = $this->Session->read('userName'); 
//validation 
$user = $this->User->find('first',array('conditions'=>array('username'=>"$username"))); 

$this->Session->write('Auth',$user); 

//now I can use: 
$this->Auth->login() 

我正在將會話保存在錯誤的索引處。