Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Set, get and delete data from cookie in magento 2

    Set, get and delete data from cookie in magento 2

    In this article we learn how to Set, get and delete data from cookie in Magento2.

    So first, Create a folder “Cookie” under app/code/Namespace/Module/ and create a file “Custom.php” under app/code/Namespace/Module/Cookie/.

    Here is the code snippet that will be written in file app/code/Namespace/Module/Cookie/Custom.php . In the following code I have get, set and delete data from remote address.:

    In the above code, I have created a function get() that is used to get data stored in cookie.
    And there is another function set(), which is used to set data in cookie with time duration, i.e. cookie unset its value after that given time duration. The function accepts two parameters:
    First parameter is $value => this contains the value which you want to set in cookie, and second parameter is $duration => contains the time duration/limit (in seconds) for cookie.

    Now about the function delete(), as it name says delete, means the function deletes/removes/unsets the cookie.

    Now we proceed to another part, i.e. how to and from where we set, get and delete cookie. To get, set and delete cookie you can use the following code snippet :

    That’s all in this article, hope it will help you, try the above code and
    if you have any issue just comment below.

    . . .

    Leave a Comment

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


  • Rafael Corrêa Gomes ♛
    Thanks for sharing!
  • Franzim Barruk
    How can I access the session data outside of magento, like if magento is in /shop and I need to access it in /forum on the same web root?
  • Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again