Jump to content

Save a session variable


bouni

Recommended Posts

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 you
Bouni




Bonjour,

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é.

Merci
Bouni

Link to comment
Share on other sites

  • 1 year later...

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 ) ;

  • Like 1
Link to comment
Share on other sites

  • 5 months later...
  • 3 weeks later...

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

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

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

  • 1 year later...

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

  • 3 years later...

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 by prestowicz (see edit history)
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...