2012-02-03 49 views
3

我遇到了會話變量問題,用戶登錄到應用程序,然後它設置會話變量,但是當它重定向到下一個控制器時,它不在那裏。cakephp重定向時丟失會話變量

目前我沒有使用auth組件,我認爲這是不正確的,但我不知道如何將它應用到我的邏輯。這是因爲我沒有使用用戶名和密碼登錄用戶,他們從其他網站獲得認證,該網站給了我一張票和一把鑰匙,以知道他們是誰。

這裏是我的UsersController的代碼,其中的應用程序啓動:

class UsuariosController extends AppController { 
public $components = array('Session'); 

function beforeFilter() { 

} 

function login() { 
    $isLogged = false; 
    if(!empty($_POST['Ffirma'])) { 
     $this->loginByTicket(); 
    } 
    else if(!empty($this->data)) { //When users log by email it works perfectly 
     $this->loginByEmail(); 
    } 
} 

private function loginByEmail() { 
    //Se busca el usuario en la base de datos 
    $u = new Usuario(); 
    $dbuser = $u->findByEmail($this->data['Usuario']['email']); 

    //if doesn't exist user in db 
    if(empty($dbuser)) { 
     $this->Session->setFlash('El usuario no existe en el sistema, consulte con el administrador.'); 
     $this->redirect(array('controller' => 'usuarios', 'action' => 'login')); 
     exit(); 
    } 
    $this->userIsCorrectlyLogged($dbuser); 
} 

function loginByTicket() { 
    $Fip = $_POST['Fip']; 
    $Frol = $_POST['Frol']; 
    $FidPersona = $_POST['Fidpersona']; 
    $Fticket = $_POST['Fticket']; 
    $Ffirma = $_POST['Ffirma']; 
    //Check sing 
    $f = $this->gen_firma($Frol, $FidPersona, $Fticket); 
    if(strcmp($f, $Ffirma) != 0) { 
     $this->Session->setFlash('Firma no válida.'); 
     return; 
    } 
    //Check if ticket is valid 
    //1º Check if it exists on the db 
    $t = split('-',$Fticket); 
    $ticket = new Ticket(); 
    $dbticket = $ticket->findById($t[0]); 
    if(strcmp($dbticket['Ticket']['valor'], $t[1]) != 0) { 
     $this->Session->setFlash('Ticket no válido.'); 
     return; 
    } 

    //2º if Ip ok 
    if($Fip != $dbticket['Ticket']['ip']) { 
     $this->Session->setFlash('IP no válida.'.' '.$dbticket['Ticket']['ip'].' '.$Fip); 
     return; 
    } 

    $u = new Usuario(); 
    $dbuser = $u->findById($dbticket['Ticket']['idPersona']); 
    $this->userIsCorrectlyLogged($dbuser); 
} 

private function userIsCorrectlyLogged($dbuser) { 
    $user = array('Usuario' => array(
     'last_login' => date("Y-m-d H:i:s"), 
     'rol_app' => 1, 
     'nombre' => $dbuser['Usuario']['nombre'], 
     'email' => $dbuser['Usuario']['email'], 
     'apellidos' => $dbuser['Usuario']['apellidos'], 
     'id' => $dbuser['Usuario']['id'] 
    )); 
    //Some stuff to determine rol privileges 
    $this->Session->destroy(); 
    $this->Session->write('Usuario', $user); 
    $this->redirect(array('controller' => 'mains', 'action' => 'index'),null, true); 
    exit(); 
} 

正如你可以看到我做一些控制,才知道該用戶正確登錄,並在用戶正確登錄我保存會話。

在我的AppController我檢查,如果用戶已經登錄,但會話變量已經走了:

class AppController extends Controller { 
    public $components = array('Session'); 
    function beforeFilter() { 
     //Configure::write('Security.level', 'medium'); //I've tried this that i saw somewhere 
     pr($this->Session->read()) // Session is empty 
     if($this->checkAdminSession()) { 
      $user = $this->Session->read('Usuario'); 
      $email = $user['Usuario']['email']; 
      $usuario = new Usuario(); 
      $dbuser = $usuario->findByEmail($email); 
      $respons = $usuario->getAccionesResponsable($dbuser['Usuario']['id']); 
      $this->set("hayacciones", true); 
      if(empty($respons)) $this->set("hayacciones", false); 
     } 
     else { 
      $this->Session->setFlash('Necesitas identificarte para acceder al sistema.'); 

      $this->redirect('/usuarios/login/'); 
      exit(); 
     } 
} 
    function checkAdminSession() { 
     return $this->Session->check('Usuario');   
    } 
} 

我絕望了,我已經讀了很多文件,但我不知道如何解決這個問題,你能給我任何線索嗎?

非常感謝你,對不起我的英語!

注:我已經發現,如果安全級別是低它的工作原理:

Configure::write('Security.level', 'low'); 

但我不喜歡這種解決方案

+0

類似:http://stackoverflow.com/questions/22079477/cakephp-session-is-lost-after-an-oauth-redirect – trante 2014-02-27 21:12:38

回答

1

我有同樣的問題。我嘗試了所有的建議。我的緩存引擎是Apc。

$this->__saveData($t); 
    debug($this->Session->read());// >>>>>> GOOD 
    $this->redirect(array('controller'=>'users','action'=>'main')); 
    } 
} 
} 
    function logout() { 
    $this->Session->destroy(); 
      $this->Session->delete('User'); 
    $this->redirect(array('controller'=>'logins','action'=>'login')); 
} 
function forgot() { 
$this->layout = 'login'; 

} 
    private function __saveData($t) 
    { 
     $this->Session->write('User',$t['User']['name']); 
     $this->Session->write('User_name',$t['User']['firstname']); 
     $this->Session->write('User_id',$t['User']['id']); 
     $this->Session->write("User_Group",$t['Group']['name']); 
     $g = $this->Myauth->getPerm('User_Group'); // This is the array of permission w.r.t to the menu (key) 
     $this->Session->write("Permissions",$g); 

     debug($this->Session->read()); 
    } 
function main() 
{ 
    // Check permissions 
$this->Myauth->check('users','login'); 
$username = $this->Session->read('User'); 
debug($this->Session->read());die(); <<<<< NOTHING 

}

有趣的是,昨天它的工作。

我的php.ini有一個簡單的extension = apc.so。 我core.php中

Configure::write('Session.defaults', 'php'); 

沒有什麼變化,如果我更改安全級別。我會欣賞任何方向。

編輯 第一個解決方案:在我的php.ini中,session.referer_check的值很差(它應該是''='0')。 但現在,在同一臺服務器上,一個站點可以。另一個觸發錯誤 錯誤:調用未定義函數apc_cache_info()

這兩個網站是分開的,不共享任何cakelib。

[溶液中發現]

蛋糕> 2.2和鉻24我發現這個溶液(I嘗試了所有的其它網絡上找到)。在你的core.php中:

 Configure::write('Security.cookie', 'cakephpfdebackend'); 
    Configure::write('Session.cookieTimeout', 0); 
    Configure::write('Session.checkAgent', false); 
    Configure::write('Session.cookie_secure',false); 
    Configure::write('Session.referer_check' ,false); 
    Configure::write('Session.defaults', 'php'); 

其實只需要Session.cookieTimeout。其他設置是可選的以解決問題。

0

我遇到了一些問題,有一些網頁會話。你可以檢查是否有任何空格在PHP結束標籤後的頁面底部。當我遇到這個問題時,我發現會話由於php結尾標記後的控制器中的空白字符而丟失。請檢查並讓我知道。

+0

檢查:php結束標記後沒有任何空格:?> I'已經檢查了模型和意見。 – NewRehtse 2012-02-03 11:05:08

+0

你確定,會話設置正確並且可以在某些頁面中訪問。或所有頁面缺少會話?我可以看到checkAdminSession()正文。我認爲這是會議正在檢查的地方。 – Kiran 2012-02-03 11:44:49

+0

目前我只有這個: 函數checkAdminSession(){ return $ this-> Session-> check('Usuario'); \t \t } – NewRehtse 2012-02-03 12:00:00

4

您正在重寫beforeFilter()方法。因此,而不是使用這樣的:

<?php 
class UsuariosController extends AppController { 

    function beforeFilter() { 

    } 

你應該這樣做:

<?php 
class UsuariosController extends AppController { 

    function beforeFilter() { 
     parent::beforeFilter(); 
    } 
4

我登錄呼叫太后丟失會話信息,並搜索了一會兒後,我發現有很多不同的方法來解決我的問題。我只感到遺憾的是我沒有完全理解是什麼導致了這個問題,但我想它與php的會話配置有關。

  1. 至於你提到的改變Security.level低固定的問題,我 配置::寫( 'Security.level', '低');
  2. 更改會話保存配置爲php修復了我的問題: Configure :: write('Session',array( 'defaults'=>'cake', ));
  3. 最後加入超時工作太(我結束了使用): 配置::寫入( '會話',陣列( '缺省'=> 'PHP', 'cookieTimeout'=> 10000 )) ;

所有這些都在/ app/Config/core中找到。php

我發佈這個希望有人能夠理解底下發生了什麼。我覺得理解問題的根源會更好地回答你的問題。

0

此問題的一個可能的原因是服務器時鐘未與客戶端的時鐘同步,因此cookie超時。