bouni Posted March 5, 2010 Share Posted March 5, 2010 Hello,I have a module on my home page which (among other things) loads a pop-up playing music on the website.I want the module to remember that the user has already seen the home page (on a session variable ?). This way, the pop-up won't appear every time the visitor returns to the home page.Can you help me ? I don't know how to set a session variable safely in Prestashop or how to use cookies efficiently.Thank youBouniBonjour,Je dispose d'un module sur ma page d'accueil qui lance un pop-up pour jouer de la musique sur le site.Mais je ne veux pas que ce pop-up apparaisse à chaque fois que le visiteur affiche la page d'accueil.Comment faire pour que le module s'en souvienne ?Variable de session ou cookie sur prestashop: je ne sais pas comment les utiliser efficacement et en sécurité.MerciBouni Link to comment Share on other sites More sharing options...
alexandru.topliceanu Posted April 27, 2011 Share Posted April 27, 2011 Hi, You can simply use the Cookie class. The way presta stores session data is different, it doesn't use php's native $_SESSION but it's own implementation of session cookies. It transports session data along with each request in an encrypted cookie.The following example sets a session var: include '/config/config.inc.php' ; include '/init.php' ; // this initializes the Cookie singleton, which is available in any script function setUserPreference( $something ){ global $cookie ; $cookie->preference = $something ; } NOTE that Cookie uses __set() magic function so you can name your properties whatever you like.All you have to do is to make sure you have the cookie object present in your context and: doSomethingWith( $cookie->preference ) ; 1 Link to comment Share on other sites More sharing options...
masbovi Posted October 5, 2011 Share Posted October 5, 2011 thanks, but with this metod I set a cookie... how can I set a session variable that expires on end of session?? Link to comment Share on other sites More sharing options...
alexandru.topliceanu Posted October 5, 2011 Share Posted October 5, 2011 Hi, It should expire just like a normal apache session. If you want to make sure the cookie is clean, check out the class Cookie.php in /classes. It has a couple of methods that might interest you: logout(), mylogout(). If this doesn't help, please give me more details of your problem. Cheers, Alex Link to comment Share on other sites More sharing options...
masbovi Posted October 27, 2011 Share Posted October 27, 2011 Alex, thanks for this reply... I have the same problem but I didn't understand how to set the expire time? I checked the Cookie.php class but I cannot understand. I want to set a cookie that expire at the end of session, when the browser is closed. Please help me thanks Link to comment Share on other sites More sharing options...
alexandru.topliceanu Posted October 28, 2011 Share Posted October 28, 2011 Hi masbovi, Two ways: at $cookie initialization which is in the core core and you shouldn't modify that OR using $cookie.setExpire(0); Behind the scenes, the class uses php's setcookie function(docs), so setting the expire to 0/NULL makes it "expire at the end of the session (when the browser closes)", taken from the docs. Sorry for the late reply. Link to comment Share on other sites More sharing options...
masbovi Posted October 28, 2011 Share Posted October 28, 2011 dear Alex please be patient with me, but I'm not a very good php programmer. My situation is that I create a new prestashop module and in my hook function i make: global $cookie; include (dirname(__FILE__).'/config/config.inc.php'); include (dirname(__FILE__).'/init.php'); $cookie->contaPagine = 1; On this way I have the cookie "contaPagine" of value 1 and expiring 20 days later. Where I have to add the $cookie.setExpire(0); ?? I want to set the expire 0 only for this cookie. I hope that I explained well thanks a lot Link to comment Share on other sites More sharing options...
tabutnas Posted June 16, 2013 Share Posted June 16, 2013 save my day :3 thanks!! Hi, You can simply use the Cookie class. The way presta stores session data is different, it doesn't use php's native $_SESSION but it's own implementation of session cookies. It transports session data along with each request in an encrypted cookie. The following example sets a session var: include '/config/config.inc.php' ; include '/init.php' ; // this initializes the Cookie singleton, which is available in any script function setUserPreference( $something ){ global $cookie ; $cookie->preference = $something ; } NOTE that Cookie uses __set() magic function so you can name your properties whatever you like. All you have to do is to make sure you have the cookie object present in your context and: doSomethingWith( $cookie->preference ) ; Link to comment Share on other sites More sharing options...
prestowicz Posted March 28, 2017 Share Posted March 28, 2017 (edited) Worth to mention, if you seek a way to keep consistent with login state and its cookies values set (update or set override method for Cookie->mylogout() method and add unset() value for added cookie value, otherwise you can run out of sync with your code if it has a complicated logic behind later). class Cookie extends CookieCore { /** * Soft logout, delete everything links to the customer * but leave there affiliate's informations. * As of version 1.5 don't call this function, use Customer::mylogout() instead; */ public function mylogout() { unset($this->_content['id_compare']); unset($this->_content['id_customer']); unset($this->_content['id_guest']); unset($this->_content['is_guest']); unset($this->_content['id_connections']); unset($this->_content['customer_lastname']); unset($this->_content['customer_firstname']); unset($this->_content['passwd']); unset($this->_content['logged']); unset($this->_content['email']); unset($this->_content['id_cart']); unset($this->_content['id_address_invoice']); unset($this->_content['id_address_delivery']); unset($this->_content['firstTimeLogged']);//custom first time logg event variable watch $this->_modified = true; } } Edited March 28, 2017 by prestowicz (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now