2

I have symfony2 application. I want to clear all session and cookies and logout application using controller. but after logingout when i click on back button in browser it returns application page instead of Login page. In PHP header is used to redirect page but in symfony i dont know how to out this problem. here is my code.

IndexAction

    $session = $this->getRequest()->getSession();
    $em = $this->getDoctrine()->getManager();
    $user = new Loginsec();


    if ($request->getMethod() == "POST") {
        $session->clear();

        $username = $request->get('user');
        $password = $request->get('pass');
        $remember = $request->get('remember');
        if ($username != "" && $password != "") {
            $Login = $this->checklogin($username, $password);
            if ($Login) {

                if ($remember != '') {
                    $response = new Response();
                    $cookiename = new Cookie('cookname',$username,time()+3600*24*7,'/LoginProject');

                    $response->headers->setcookie($cookiename);
                    $response->setContent($cookiename);
                    $response->send();
                }


                //Manage Session

                $user->setUser($username);           
                $session->set('user',$username);
                $session->start();

                return $this->render('LoginLoginBundle:Default:Company.html.twig', array('name' => $session->get('user'));
            } else {

                return $this->render('LoginLoginBundle:Default:Login.html.twig', array('error' => 'Login Failed'));
            }
        } else {
            return $this->render('LoginLoginBundle:Default:Login.html.twig', array('error' => 'Input required Fields'));
        }
    } else {

         //Get SESSION
        if ($session->has('user') && $session->has('pass')) {

            $login = $this->checklogin($session->get('user'), $session->get('pass'));

            if($login){
                      return $this->render('LoginLoginBundle:Default:Company.html.twig', array('name' => $session->get('user'));


            }

        }elseif($request->cookies->get('cookname')){

                //Get Cookie
                $request=  $this->get('request');
                $login = $this->checklogin($request->cookies->get('cookname'),$request->cookies->get('cookpass')) ;
                if($login){
                    return $this->render('LoginLoginBundle:Default:Login.html.twig', array('error' => 'Login Using Cookie'));
                }

            } else {
                return $this->render('LoginLoginBundle:Default:Login.html.twig');
            }
    }

logoutAction

Public function logoutAction(Request $request) {

    $session = $this->getRequest()->getSession();
    $session->clear('user');
    $session->remove('user');
    unset($session);

    $response = new Response();
    $response->headers->clearCookie('cookname') ;
    $response->send();

    return $this->render('LoginLoginBundle:Default:Login.html.twig');

}

Please help me if any Idea.

Dani Sancas
  • 1,365
  • 11
  • 27
Gopal Joshi
  • 2,350
  • 22
  • 49
  • I had similar problems, but with the built login/logout Symfony2 system. I found then an answer to it and was the `invalidate session` parameter inside the config. Maybe could be useful for you too. Look for `invalidate()` at the `Session API` block: http://symfony.com/doc/current/components/http_foundation/sessions.html – Dani Sancas Jan 24 '14 at 07:20
  • possible duplicate of [(PHP) How to destroy the session cookie correctly?](http://stackoverflow.com/questions/2241769/php-how-to-destroy-the-session-cookie-correctly) – Peon Jan 24 '14 at 07:21
  • 3
    I don't think it's a duplicate, because @Sameer is doing it with Symfony, and it has it's own methods. With Symfony you don't handle sessions directly, you do it through Symfony2 built methods. – Dani Sancas Jan 24 '14 at 07:27
  • Hey Dainis, @DaniSancas is right in php,i can handle session operation but in symfony, Methods are diffrent then php methods. – Gopal Joshi Jan 24 '14 at 07:31
  • Did my advice work? Hope it did! – Dani Sancas Jan 24 '14 at 07:33
  • Thankx bro but i can not understand what to do.will you reply me syntex.? – Gopal Joshi Jan 24 '14 at 07:41
  • $session->invalidate() – redbirdo Jan 24 '14 at 11:57
  • @redbirdo thanks but it not work.It logout but back to profile page on clicking on back button of brower. – Gopal Joshi Jan 24 '14 at 12:41
  • Yes, but if the user submits anything the session will be invalid and then it should redirect to login. I'm not sure what you mean by "in php header is used to redirect page". Do you mean set the header to prevent page caching, in which case back causes a page reload, which indirectly redirects to login? If so, that has nothing to do with the session, it's the page headers. – redbirdo Jan 24 '14 at 13:09
  • Have a look at http://symfony.com/doc/current/book/http_cache.html. You should be able to modify the profile page response (after render, before return) to disable caching. – redbirdo Jan 24 '14 at 13:18
  • I had this issue and added `invalidate_session: true` to the logout section of my firewall in `security.yml` (see: line 180 - http://symfony.com/doc/current/reference/configuration/security.html). I have recently changed to using the PDO session handler (http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html) which has somehow stopped this from being an issue. Not sure if it's the PDO handler or just the multiple updates that I've done between setting `invalidate_session` and realising it wasn't needed anymore. – qooplmao Jan 25 '14 at 00:25
  • $redbirdo I exactly mean set the header to prevent page caching, in which case back causes a page reload, which indirectly redirects to login – Gopal Joshi Jan 25 '14 at 06:53

1 Answers1

1

I was just reviewing my questions and found this one. Actually it's solved long time ago.

Unset session variable using

$session->remove('<Parameter>');

Ex.

$session->remove('user');
Gopal Joshi
  • 2,350
  • 22
  • 49