David Aclub
David Aclub

Reputation: 105

Prestashop 1.6 session/cookie with smarty

In my prestashop a user (that is not a client or an admin) can create a "side" account (that is not a prestashop account) to do something special on the site.

I have created the everything to do that but when the user connect i can not keep the data during the session.

I've look for a way to keep the data and the only thing i see is the smarty cookie. Fine by me BUT i can not control the lifetime of that cookie. And i need this cookie to die when the user close the browser.

So i've tried to do a session, but i can't get it to work and i didn't see a way to do a cookie that doesn't last.

Anyone has an idea to do a session like data, or to handle the cookie lifespan?

Thank you

Upvotes: 5

Views: 15271

Answers (4)

Mr.D1rk
Mr.D1rk

Reputation: 1

You can use SESSION in Prestashop only with this parameters $_SESSION['VIEW'] Other sessions has be destroy

Upvotes: 0

Kunal Panchal
Kunal Panchal

Reputation: 1059

Tried with Prestashop 1.6.1.x

Original Post with few corrections http://vblanch.com/get-the-contact-email-in-prestashop-shop-name-and-set-values-in-cookies/

If you also need to put values into cookies:

$this->context->cookie->__set('name_of_your_key', $your_value);

To get the value from a smarty template (.tpl):

{$cookie->name_of_your_key}

From PHP (inside a controller):

$this->context->cookie->name_of_your_key

Outside of a controller:

$context = Context::getContext();
$context->cookie->name_of_your_key;

Upvotes: 0

unloco
unloco

Reputation: 7320

You can use the CookieCore class

//to write
$cookie = new Cookie('my_cookie'); //make your own cookie
$cookie->setExpire(time() + 20 * 60); // 20 minutes for example
$cookie->variable_name = 'hello';
$cookie->write();

//to read
$cookie = new Cookie('my_cookie');
echo $cookie->variable_name;
//hello

Upvotes: 9

David Aclub
David Aclub

Reputation: 105

I gonna add to the message of UnLoCo.

For people looking for a cookie that die at the end of the session, just put the

$cookie->setExpire(0);

NB the cookie will die only when the browser is completly close (i have an extention feedly that let the browser open, so i though there was a bug)

And last thing if you want to kill the cookie yourself

$cookie = new Cookie('my_cookie');
$cookie->variable_name = null;
$cookie->write();

Upvotes: 3

Related Questions