How to add Cookie Magento 2?












13















I want to add a COOKIE with value and some duration, how to do it in magento 2.



Thanks










share|improve this question





























    13















    I want to add a COOKIE with value and some duration, how to do it in magento 2.



    Thanks










    share|improve this question



























      13












      13








      13


      2






      I want to add a COOKIE with value and some duration, how to do it in magento 2.



      Thanks










      share|improve this question
















      I want to add a COOKIE with value and some duration, how to do it in magento 2.



      Thanks







      magento2 cookie magento-2.0






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 5 '17 at 4:31









      Dinesh Yadav

      3,9751937




      3,9751937










      asked Feb 3 '16 at 3:36









      J. JohnJ. John

      192312




      192312






















          2 Answers
          2






          active

          oldest

          votes


















          18














          IMO the best approach would be to create a class to wrap the cookie creation and then just use it where you want.



          The Cookie Class




          {Vendor}/{Module}/Cookie/Example.php




          <?php 

          namespace VendorModuleCookie;

          use MagentoFrameworkStdlibCookieManagerInterface;
          use MagentoFrameworkStdlibCookieCookieMetadataFactory;
          use MagentoFrameworkSessionSessionManagerInterface;

          class Example
          {
          /**
          * Name of cookie that holds private content version
          */
          const COOKIE_NAME = 'example';

          /**
          * CookieManager
          *
          * @var CookieManagerInterface
          */
          private $cookieManager;

          /**
          * @var CookieMetadataFactory
          */
          private $cookieMetadataFactory;

          /**
          * @var SessionManagerInterface
          */
          private $sessionManager;

          /**
          * @param CookieManagerInterface $cookieManager
          * @param CookieMetadataFactory $cookieMetadataFactory
          * @param SessionManagerInterface $sessionManager
          */
          public function __construct(
          CookieManagerInterface $cookieManager,
          CookieMetadataFactory $cookieMetadataFactory,
          SessionManagerInterface $sessionManager
          ) {
          $this->cookieManager = $cookieManager;
          $this->cookieMetadataFactory = $cookieMetadataFactory;
          $this->sessionManager = $sessionManager;
          }

          /**
          * Get form key cookie
          *
          * @return string
          */
          public function get()
          {
          return $this->cookieManager->getCookie(self::COOKIE_NAME);
          }

          /**
          * @param string $value
          * @param int $duration
          * @return void
          */
          public function set($value, $duration = 86400)
          {
          $metadata = $this->cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration($duration)
          ->setPath($this->sessionManager->getCookiePath())
          ->setDomain($this->sessionManager->getCookieDomain());

          $this->cookieManager->setPublicCookie(
          self::COOKIE_NAME,
          $value,
          $metadata
          );
          }

          /**
          * @return void
          */
          public function delete()
          {
          $metadata = $this->cookieMetadataFactory
          ->createPublicCookieMetadata()
          ->setDuration($duration)
          ->setPath($this->sessionManager->getCookiePath())
          ->setDomain($this->sessionManager->getCookieDomain());

          $this->cookieManager->deleteCookie(
          self::COOKIE_NAME,
          $metadata
          );
          }
          }



          This example was based on MagentoFrameworkAppPageCacheFormKey
          and represent a single cookie with name "example"




          If you want to add some custom properties to the $metadata (PublicCookieMetadata) as change the path,http_only, etc you should refactor the set() and/or delete() method(s).



          How to use it



          You can access that class simply using the Object Manager in almost anywhere (Ugly Approach):



          $objectManager = MagentoFrameworkAppObjectManager::getInstance();
          $objectManager->get('VendorModuleCookieExample')
          ->set('value', 3600);


          Depending "where" you need to create the cookie you can take a look at the constructor of your class maybe you already have an Object manager there, if not you can also inject it in the constructor.






          share|improve this answer


























          • @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

            – MauroNigrele
            Feb 3 '16 at 6:03






          • 3





            I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

            – R T
            Apr 12 '16 at 10:39











          • what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

            – LucScu
            Jun 1 '16 at 8:17











          • @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

            – Shivam
            Jun 9 '16 at 6:24






          • 1





            Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

            – spiil
            Aug 3 '17 at 11:10



















          0














          You can set & get cookie using php basic function like this:



          //set cookie
          $cookie_name = "magento";
          $cookie_value = "How to Cookie";
          setcookie($cookie_name, $cookie_value, time()+3600); /* expire in 1 hour */

          //get cookie
          if(!isset($_COOKIE[$cookie_name])) {
          echo "Cookie named '" . $cookie_name . "' is not set!";
          } else {
          echo "Cookie '" . $cookie_name . "' is set!<br>";
          echo "Value is: " . $_COOKIE[$cookie_name];
          }





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f100136%2fhow-to-add-cookie-magento-2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            18














            IMO the best approach would be to create a class to wrap the cookie creation and then just use it where you want.



            The Cookie Class




            {Vendor}/{Module}/Cookie/Example.php




            <?php 

            namespace VendorModuleCookie;

            use MagentoFrameworkStdlibCookieManagerInterface;
            use MagentoFrameworkStdlibCookieCookieMetadataFactory;
            use MagentoFrameworkSessionSessionManagerInterface;

            class Example
            {
            /**
            * Name of cookie that holds private content version
            */
            const COOKIE_NAME = 'example';

            /**
            * CookieManager
            *
            * @var CookieManagerInterface
            */
            private $cookieManager;

            /**
            * @var CookieMetadataFactory
            */
            private $cookieMetadataFactory;

            /**
            * @var SessionManagerInterface
            */
            private $sessionManager;

            /**
            * @param CookieManagerInterface $cookieManager
            * @param CookieMetadataFactory $cookieMetadataFactory
            * @param SessionManagerInterface $sessionManager
            */
            public function __construct(
            CookieManagerInterface $cookieManager,
            CookieMetadataFactory $cookieMetadataFactory,
            SessionManagerInterface $sessionManager
            ) {
            $this->cookieManager = $cookieManager;
            $this->cookieMetadataFactory = $cookieMetadataFactory;
            $this->sessionManager = $sessionManager;
            }

            /**
            * Get form key cookie
            *
            * @return string
            */
            public function get()
            {
            return $this->cookieManager->getCookie(self::COOKIE_NAME);
            }

            /**
            * @param string $value
            * @param int $duration
            * @return void
            */
            public function set($value, $duration = 86400)
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
            );
            }

            /**
            * @return void
            */
            public function delete()
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $metadata
            );
            }
            }



            This example was based on MagentoFrameworkAppPageCacheFormKey
            and represent a single cookie with name "example"




            If you want to add some custom properties to the $metadata (PublicCookieMetadata) as change the path,http_only, etc you should refactor the set() and/or delete() method(s).



            How to use it



            You can access that class simply using the Object Manager in almost anywhere (Ugly Approach):



            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $objectManager->get('VendorModuleCookieExample')
            ->set('value', 3600);


            Depending "where" you need to create the cookie you can take a look at the constructor of your class maybe you already have an Object manager there, if not you can also inject it in the constructor.






            share|improve this answer


























            • @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

              – MauroNigrele
              Feb 3 '16 at 6:03






            • 3





              I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

              – R T
              Apr 12 '16 at 10:39











            • what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

              – LucScu
              Jun 1 '16 at 8:17











            • @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

              – Shivam
              Jun 9 '16 at 6:24






            • 1





              Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

              – spiil
              Aug 3 '17 at 11:10
















            18














            IMO the best approach would be to create a class to wrap the cookie creation and then just use it where you want.



            The Cookie Class




            {Vendor}/{Module}/Cookie/Example.php




            <?php 

            namespace VendorModuleCookie;

            use MagentoFrameworkStdlibCookieManagerInterface;
            use MagentoFrameworkStdlibCookieCookieMetadataFactory;
            use MagentoFrameworkSessionSessionManagerInterface;

            class Example
            {
            /**
            * Name of cookie that holds private content version
            */
            const COOKIE_NAME = 'example';

            /**
            * CookieManager
            *
            * @var CookieManagerInterface
            */
            private $cookieManager;

            /**
            * @var CookieMetadataFactory
            */
            private $cookieMetadataFactory;

            /**
            * @var SessionManagerInterface
            */
            private $sessionManager;

            /**
            * @param CookieManagerInterface $cookieManager
            * @param CookieMetadataFactory $cookieMetadataFactory
            * @param SessionManagerInterface $sessionManager
            */
            public function __construct(
            CookieManagerInterface $cookieManager,
            CookieMetadataFactory $cookieMetadataFactory,
            SessionManagerInterface $sessionManager
            ) {
            $this->cookieManager = $cookieManager;
            $this->cookieMetadataFactory = $cookieMetadataFactory;
            $this->sessionManager = $sessionManager;
            }

            /**
            * Get form key cookie
            *
            * @return string
            */
            public function get()
            {
            return $this->cookieManager->getCookie(self::COOKIE_NAME);
            }

            /**
            * @param string $value
            * @param int $duration
            * @return void
            */
            public function set($value, $duration = 86400)
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
            );
            }

            /**
            * @return void
            */
            public function delete()
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $metadata
            );
            }
            }



            This example was based on MagentoFrameworkAppPageCacheFormKey
            and represent a single cookie with name "example"




            If you want to add some custom properties to the $metadata (PublicCookieMetadata) as change the path,http_only, etc you should refactor the set() and/or delete() method(s).



            How to use it



            You can access that class simply using the Object Manager in almost anywhere (Ugly Approach):



            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $objectManager->get('VendorModuleCookieExample')
            ->set('value', 3600);


            Depending "where" you need to create the cookie you can take a look at the constructor of your class maybe you already have an Object manager there, if not you can also inject it in the constructor.






            share|improve this answer


























            • @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

              – MauroNigrele
              Feb 3 '16 at 6:03






            • 3





              I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

              – R T
              Apr 12 '16 at 10:39











            • what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

              – LucScu
              Jun 1 '16 at 8:17











            • @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

              – Shivam
              Jun 9 '16 at 6:24






            • 1





              Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

              – spiil
              Aug 3 '17 at 11:10














            18












            18








            18







            IMO the best approach would be to create a class to wrap the cookie creation and then just use it where you want.



            The Cookie Class




            {Vendor}/{Module}/Cookie/Example.php




            <?php 

            namespace VendorModuleCookie;

            use MagentoFrameworkStdlibCookieManagerInterface;
            use MagentoFrameworkStdlibCookieCookieMetadataFactory;
            use MagentoFrameworkSessionSessionManagerInterface;

            class Example
            {
            /**
            * Name of cookie that holds private content version
            */
            const COOKIE_NAME = 'example';

            /**
            * CookieManager
            *
            * @var CookieManagerInterface
            */
            private $cookieManager;

            /**
            * @var CookieMetadataFactory
            */
            private $cookieMetadataFactory;

            /**
            * @var SessionManagerInterface
            */
            private $sessionManager;

            /**
            * @param CookieManagerInterface $cookieManager
            * @param CookieMetadataFactory $cookieMetadataFactory
            * @param SessionManagerInterface $sessionManager
            */
            public function __construct(
            CookieManagerInterface $cookieManager,
            CookieMetadataFactory $cookieMetadataFactory,
            SessionManagerInterface $sessionManager
            ) {
            $this->cookieManager = $cookieManager;
            $this->cookieMetadataFactory = $cookieMetadataFactory;
            $this->sessionManager = $sessionManager;
            }

            /**
            * Get form key cookie
            *
            * @return string
            */
            public function get()
            {
            return $this->cookieManager->getCookie(self::COOKIE_NAME);
            }

            /**
            * @param string $value
            * @param int $duration
            * @return void
            */
            public function set($value, $duration = 86400)
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
            );
            }

            /**
            * @return void
            */
            public function delete()
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $metadata
            );
            }
            }



            This example was based on MagentoFrameworkAppPageCacheFormKey
            and represent a single cookie with name "example"




            If you want to add some custom properties to the $metadata (PublicCookieMetadata) as change the path,http_only, etc you should refactor the set() and/or delete() method(s).



            How to use it



            You can access that class simply using the Object Manager in almost anywhere (Ugly Approach):



            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $objectManager->get('VendorModuleCookieExample')
            ->set('value', 3600);


            Depending "where" you need to create the cookie you can take a look at the constructor of your class maybe you already have an Object manager there, if not you can also inject it in the constructor.






            share|improve this answer















            IMO the best approach would be to create a class to wrap the cookie creation and then just use it where you want.



            The Cookie Class




            {Vendor}/{Module}/Cookie/Example.php




            <?php 

            namespace VendorModuleCookie;

            use MagentoFrameworkStdlibCookieManagerInterface;
            use MagentoFrameworkStdlibCookieCookieMetadataFactory;
            use MagentoFrameworkSessionSessionManagerInterface;

            class Example
            {
            /**
            * Name of cookie that holds private content version
            */
            const COOKIE_NAME = 'example';

            /**
            * CookieManager
            *
            * @var CookieManagerInterface
            */
            private $cookieManager;

            /**
            * @var CookieMetadataFactory
            */
            private $cookieMetadataFactory;

            /**
            * @var SessionManagerInterface
            */
            private $sessionManager;

            /**
            * @param CookieManagerInterface $cookieManager
            * @param CookieMetadataFactory $cookieMetadataFactory
            * @param SessionManagerInterface $sessionManager
            */
            public function __construct(
            CookieManagerInterface $cookieManager,
            CookieMetadataFactory $cookieMetadataFactory,
            SessionManagerInterface $sessionManager
            ) {
            $this->cookieManager = $cookieManager;
            $this->cookieMetadataFactory = $cookieMetadataFactory;
            $this->sessionManager = $sessionManager;
            }

            /**
            * Get form key cookie
            *
            * @return string
            */
            public function get()
            {
            return $this->cookieManager->getCookie(self::COOKIE_NAME);
            }

            /**
            * @param string $value
            * @param int $duration
            * @return void
            */
            public function set($value, $duration = 86400)
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
            );
            }

            /**
            * @return void
            */
            public function delete()
            {
            $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());

            $this->cookieManager->deleteCookie(
            self::COOKIE_NAME,
            $metadata
            );
            }
            }



            This example was based on MagentoFrameworkAppPageCacheFormKey
            and represent a single cookie with name "example"




            If you want to add some custom properties to the $metadata (PublicCookieMetadata) as change the path,http_only, etc you should refactor the set() and/or delete() method(s).



            How to use it



            You can access that class simply using the Object Manager in almost anywhere (Ugly Approach):



            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $objectManager->get('VendorModuleCookieExample')
            ->set('value', 3600);


            Depending "where" you need to create the cookie you can take a look at the constructor of your class maybe you already have an Object manager there, if not you can also inject it in the constructor.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 35 mins ago









            Ahmad Santarissy

            1033




            1033










            answered Feb 3 '16 at 5:00









            MauroNigreleMauroNigrele

            2,577926




            2,577926













            • @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

              – MauroNigrele
              Feb 3 '16 at 6:03






            • 3





              I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

              – R T
              Apr 12 '16 at 10:39











            • what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

              – LucScu
              Jun 1 '16 at 8:17











            • @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

              – Shivam
              Jun 9 '16 at 6:24






            • 1





              Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

              – spiil
              Aug 3 '17 at 11:10



















            • @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

              – MauroNigrele
              Feb 3 '16 at 6:03






            • 3





              I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

              – R T
              Apr 12 '16 at 10:39











            • what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

              – LucScu
              Jun 1 '16 at 8:17











            • @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

              – Shivam
              Jun 9 '16 at 6:24






            • 1





              Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

              – spiil
              Aug 3 '17 at 11:10

















            @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

            – MauroNigrele
            Feb 3 '16 at 6:03





            @J. John, Hey! i juts saw that you accept the answer while i've been editing it, just take a look now, because i made several changes in order to simplify the implementation.

            – MauroNigrele
            Feb 3 '16 at 6:03




            3




            3





            I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

            – R T
            Apr 12 '16 at 10:39





            I've to edit createCookieMetaData() to createPublicCookieMetadata() in set method to resolve the error (fatal error). Smart approach though!

            – R T
            Apr 12 '16 at 10:39













            what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

            – LucScu
            Jun 1 '16 at 8:17





            what's the difference between MagentoFrameworkSessionSessionManagerInterface used by you and MagentoFrameworkSessionConfigConfigInterface used in MagentoFrameworkSessionSessionManager?

            – LucScu
            Jun 1 '16 at 8:17













            @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

            – Shivam
            Jun 9 '16 at 6:24





            @MauroNigrele function call in set and delete method should be createPublicCookieMetadata instead of createCookieMetadata

            – Shivam
            Jun 9 '16 at 6:24




            1




            1





            Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

            – spiil
            Aug 3 '17 at 11:10





            Guys. I've proposed changes because $this->createPublicCookieMetadata() doesn't exist in this class. It should be $this->cookieMetadataFactory->createPublicCookieMetadata() See in magento repo. But I've get 2 rejects. LOL...

            – spiil
            Aug 3 '17 at 11:10













            0














            You can set & get cookie using php basic function like this:



            //set cookie
            $cookie_name = "magento";
            $cookie_value = "How to Cookie";
            setcookie($cookie_name, $cookie_value, time()+3600); /* expire in 1 hour */

            //get cookie
            if(!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
            } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
            }





            share|improve this answer




























              0














              You can set & get cookie using php basic function like this:



              //set cookie
              $cookie_name = "magento";
              $cookie_value = "How to Cookie";
              setcookie($cookie_name, $cookie_value, time()+3600); /* expire in 1 hour */

              //get cookie
              if(!isset($_COOKIE[$cookie_name])) {
              echo "Cookie named '" . $cookie_name . "' is not set!";
              } else {
              echo "Cookie '" . $cookie_name . "' is set!<br>";
              echo "Value is: " . $_COOKIE[$cookie_name];
              }





              share|improve this answer


























                0












                0








                0







                You can set & get cookie using php basic function like this:



                //set cookie
                $cookie_name = "magento";
                $cookie_value = "How to Cookie";
                setcookie($cookie_name, $cookie_value, time()+3600); /* expire in 1 hour */

                //get cookie
                if(!isset($_COOKIE[$cookie_name])) {
                echo "Cookie named '" . $cookie_name . "' is not set!";
                } else {
                echo "Cookie '" . $cookie_name . "' is set!<br>";
                echo "Value is: " . $_COOKIE[$cookie_name];
                }





                share|improve this answer













                You can set & get cookie using php basic function like this:



                //set cookie
                $cookie_name = "magento";
                $cookie_value = "How to Cookie";
                setcookie($cookie_name, $cookie_value, time()+3600); /* expire in 1 hour */

                //get cookie
                if(!isset($_COOKIE[$cookie_name])) {
                echo "Cookie named '" . $cookie_name . "' is not set!";
                } else {
                echo "Cookie '" . $cookie_name . "' is set!<br>";
                echo "Value is: " . $_COOKIE[$cookie_name];
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 27 '18 at 4:17









                HunterHunter

                786423




                786423






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f100136%2fhow-to-add-cookie-magento-2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Polycentropodidae

                    Magento 2 Error message: Invalid state change requested

                    Paulmy