Phalcon Cookie Management

Cookie is a small text file stored by browser on user system. Cookies were designed to be a reliable mechanism for websites to remember stateful information or to record the user’s browsing activity. Phalcon\Http\Response\Cookies is the directory where cookies are stored. Cookie management helps in storing cookies under the above directory during the request execution and is sent automatically at the end of the request.

Syntax:

To check previously set cookie: $var_name->cookies->has(‘checking_name’);

Get the cookie: $var_name->cookies->get(‘cookie_name’);

Get cookie value: $var_name->cookies->getValue();

Set the cookie: $var_name ->cookies->set(cookie_name, ‘some value’, time() + 15 * 86400);

Delete the cookie: $var_name ->cookies->get(‘cookie_name ‘)->delete();

Example of Cookie generation

<?php  

  

use Phalcon\Mvc\Controller;  

  

class SessionController extends Controller  

{  

    public function loginAction()  

    {  

        // Check if the cookie has previously set  

        if ($this->cookies->has('cookie-name')) {  

            // Get the cookie  

            $rememberMeCookie = $this->cookies->get('cookie-name');  

  

            // Get the cookie's value  

            $value = $rememberMeCookie->getValue();  

        }  

    }  

  

    public function startAction()  

    {  

        $this->cookies->set(  

            'cookie-name',  

            'some value',  

            time() + 15 * 86400  

        );  

    }  

  

    public function logoutAction()  

    {  

        $rememberMeCookie = $this->cookies->get('cookie-name');  

  

        // Delete the cookie  

        $rememberMeCookie->delete();  

    }  

}

On above code execution following files are generated

Phalcon Cookie 1

Now implement and generate cookie from following code: In this cookie name is “login-action” with value “javatpoint”.

  <?php    

  

class UsersController extends \Phalcon\Mvc\Controller {   

   public function indexAction() {   

      if ($this->cookies->has("login-action")) {   

         // Get the cookie   

         $loginCookie = $this->cookies->get("login-action");   

           

         // Get the cookie's value   

         $value = $loginCookie->getValue();   

         echo($value);   

      }   

      $this->cookies->set(   

         "login-action",   

         "javatpoint",   

         time() + 15 * 86400   

      );   

   }   

}  

    Output

    Phalcon Cookie 2

    Encryption/Decryption of Cookies

    Cookies contain the important information about the user browsing that’s why by default it is encrypted. It is encrypted when sent to client and decrypted when retrieved by the user.

    To disable the encryption we can do following changes in the code:

    <?php  
    
      
    
    use Phalcon\Http\Response\Cookies;  
    
      
    
    $di->set(  
    
        'cookies',  
    
        function () {  
    
            $cookies = new Cookies();  
    
      
    
            $cookies->useEncryption(false);  
    
      
    
            return $cookies;  
    
        }  
    
    );

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *