We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session not working

Hi,

In IndexController, signin action -- where session gets created and sets some sessiom variables. Defined an hyperlink in signin.phtml, that redirects to welcome action in Index Controller. Trying to get session variables in welcome action. But, no luck and its not working.

Please, find the source code link below: TempApp


//index.php

    $di->setShared(
    'session',
    function () {
        ini_set('session.gc_maxlifetime', 3600);
        session_set_cookie_params(3600);
        $session = new Session(
            [
                "uniqueId" => "my-app-1",
            ]
        );

        $session->start();

        return $session;
    }
    );

//indexcontroller.php

          public function signinAction(){
              session_start([
                  'cookie_lifetime' => 86400,
              ]);
              echo "SignIn Action";
              $this->session->set('user-name', 'kkk');
              $_SESSION['user'] = 'green';

          }

          public function welcomeAction(){

              // Check if the variable is defined
              if ($this->session->has('user-name')) {
                  // Retrieve its value
                  $name = $this->session->get('user-name');
                  echo $name;
              }
              var_dump($this->session);

              var_dump($_SESSION);
          }

Thanks Karthik K



85.5k
Accepted
answer
edited Dec '17

Hi. Marry Christmas.

you need to regiset session servise rather thean starting the session yourself

https://docs.phalcon.io/fr/3.2/session

Stop using any session_/set_ini functions