2013-07-29 123 views
0

我想在Symfony2会话之前将sesion_liftime延长。在Symfony2中延长会话生存期

我向用户显示他将在几秒钟内注销,并且当他/她确认要扩展该会话时,我会向控制器发送一个ajax请求,以便扩展该会话。但问题是请求不起作用。

我尝试了几个不同的解决方案,但都没有工作。

<script type="text/javascript"> 
      var timeoutID; 

      function delayedAlert() { 
       timeoutID = window.setTimeout(slowAlert, 15000); 
      } 

      function slowAlert() { 
       setTimeout(logout, 5500) 
       var r=confirm("You will be logout in 5 seconds!"); 
       if (r==true) 
       { 
       $.ajax({ 
        url: 'http://localhost/interactivo/web/app_dev.php/check', 
        xhrFields: { 
         withCredentials: true 
        } 
       }); 
       alert("You wont be logout"); 
       } 
      } 

      function logout() {alert("You are logout");}; 

      delayedAlert(); 
     </script> 


/** 
* @Route("/check") 
*/ 
public function indexAction() 
{ 
//  FIRST ATTEMPT 
//  $value = 'something from somewhere'; 
// 

//  setcookie("TestCookie", $value); 
//  setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */ 
//  setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1); 

//  SECOND ATTEMPT 
//  header('Content-type: text/html; charset=utf-8'); 
//  echo 'TEST'; 

//  THIRD ATTEMPT 
     $session = $this->container->get('session'); 

     $lastUsed = new \DateTime(); 
     $lastUsed->setTimestamp($session->getMetadataBag()->getLastUsed()); 

     return $this->render('GLHomeBundle:Default:sesion.html.twig', array('entities'=> $lastUsed)); 
    } 

我做错了什么?

回答

0

我认为这不是一个好的解决方案(如果它有效),但symfony中会话的cookie被称为“PHPSESSID”。试着将这个cookie的使用时间设置为所需的时间?我使用这种方法将Drupal和Varnish中的会话传递给客户端,所以我认为它可以延长会话的生命周期。

问候, Peekmo

+0

的问题是,这个Ajax请求不调用控制器,我已经发现,如果我只会使用会话,它会自动更改lastModified的属性,所以iw将足以延长会话。但我怎么能调用这个indexAction? – user2576422

+0

url:'/ check',这是肯定的,你可以通过使用url中的路由来调用你的控制器,如果它不起作用,你的问题在其他地方 – Peekmo

0

我finaly找到此解决方案。我使用了这个site中的SessionKeepAliveListener。 我打电话通过AJAX控制器是这样的:

function slowAlert() { 
      setTimeout(logout, 40000) 
      var r=confirm("You will be logout in 20s!"); 
      if (r==true) 
      { 
      $.ajax({ 
       url: "{{ url('_demo_hello') }}", 
       xhrFields: { 
        withCredentials: true 
       } 
      }); 
      alert("You wont be logout"); 
      } 
     } 

控制器是很容易的:d

/** 
* @Route("/check", name="_demo_hello") 
*/ 
public function checkAction() 
{  
    $response = new Response(); 

    return $response; 
}