重定向时 cakephp 丢失了会话变量 [英] cakephp lost session variable when redirect

查看:16
本文介绍了重定向时 cakephp 丢失了会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的会话变量有问题,用户登录应用程序,然后它设置了一个会话变量,但当它重定向到下一个控制器时,它就不存在了.

I have problems with a session variable, users log into the app and then it sets a session variable but when it redirects to the next controller it isn't there.

目前我没有使用auth组件,我认为它不正确,但我不知道如何将其应用于我的逻辑.这是因为我没有使用用户名和密码登录用户,他们来自其他网站的身份验证,这给了我一张票和一个知道他们是谁的钥匙.

At the moment I am not using the auth component, I think it is not correct, but I don't know how to apply it to my logic. This is because I dont log in users with username and password, they come authenticated from other website that gives me a ticket and a key to know who they are.

这是我的应用程序启动的 UsersController 代码:

Here is my code of the UsersController where the app starts:

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();
}

如您所见,我在知道用户已正确登录之前进行了一些控制,而在用户正确登录时,我只保存会话.

As you can see I make some controls before know that the user is correctly logged, and in user correctly logged I just save the session.

在我的 AppController 中,我检查用户是否已登录,但会话变量已经消失:

In my AppController I check if the user has logged in, but the session variable has already gone:

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');        
    }
}

我很绝望,我已经阅读了很多文档,但我不知道如何解决这个问题,你能给我一些线索吗?

I'm desperate, I've read a lot of documentation but I don't know how to solve this problem, could you give me any clue?

非常感谢你,对不起我的英语!

Thanks you very much, and sorry for my English!.

注意:我发现如果安全级别低它可以工作:

Note: I have discovered that if the security level is low it works:

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

但我不喜欢这个解决方案...

But I dont like this solution...

推荐答案

我有同样的问题.我尝试了所有的建议.我的缓存引擎是 Apc.

I have the same problem. I tried all the suggestion. My Cache engine is 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

}

有趣的是,昨天它奏效了.

The funny thing is that yesterday it worked.

我的 php.ini 有一个简单的扩展名=apc.so.我的core.php

My php.ini has a simple extension=apc.so. My core.php

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

如果我更改安全级别,则不会发生任何变化.我会欣赏任何方向.

Nothing change if I change the Security level. I will appreciate any direction.

编辑第一个解决方案:在我的 php.ini 中,session.referer_check 的值不正确(它应该是 0,而应该是 '').但是现在,在同一台服务器上,一个站点就可以了.另一个触发错误错误:调用未定义的函数 apc_cache_info()

EDIT First solution: in my php.ini I had a bad value for session.referer_check (It was = 0 while it should be ''). But now, on the same server, one site is ok. Another one fires the error Error: Call to undefined function apc_cache_info()

这两个站点是分开的,不共享任何 cakelib.

The two sites are separated and do not share any cakelib.

[找到解决方案]

对于 Cake > 2.2 和 Chrome 24,我找到了这个解决方案(我尝试了在网上找到的所有其他解决方案).在您的 core.php 中:

For Cake > 2.2 and Chrome 24 I found this solution (I tried all the others found on the web). In your 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.其他设置是可选的以解决问题.

Actually, only the Session.cookieTimeout is required. The other settings are optional to solve the problem.

这篇关于重定向时 cakephp 丢失了会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆