8

I would like to set a "destination" cookie for all users on my site, to serve tailored content. The cookie needs to persist across login/logout so it appears I cannot use jSession, which is cleared on login/logout.

I can sort of achieve this the old fashioned way

setcookie( "destinationcookie", $_POST['destination'], strtotime( '+90 days' ) );
$destination =  isset($_POST['destination']) ? $_POST['destination'] : $_COOKIE["destinationcookie"];

...but I'm sure joomla has a better way hidden away somewhere. PS - the documentation on this is practically non-existent as far as I can find.

  • JSession needs a cookie to recognize a session, but they are not the same thing. – Valentin Despa May 11 '14 at 18:46
5

You may want to have a look at the cookie authentication plugin. It sets and reads a persistent cookie.

It's found here: https://github.com/joomla/joomla-cms/tree/staging/plugins/authentication/cookie

To read a cookie, you can use

$app = JFactory::getApplication();
$cookieValue = $app->input->cookie->get($cookieName);

To set a cookie, you use

$app = JFactory::getApplication();
$app->input->cookie->set($cookieName, $cookieValue, time() + $lifetime, $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection());

Some documentation can be found on the API page: http://api.joomla.org/cms-3/classes/JInputCookie.html

  • Thanks. trying to understand this. The documentation is mosyly indecipherable to novices like me. Could you possibly show me how the set cookie applies specifically in my case? how do I set lifetime and what is the ssl reference for? – user2097091 May 11 '14 at 16:05
  • In this case, $lifetime is coming from a parameter in the cookie plugin. The argument is the time when the cookie will expire. You just pass your own value there. The SSL argument says if the cookie will only be valid over SSL connections. In this case it just looks if the page is accessed using SSL and sets the secure flag in the cookie accordingly. – Bakual May 11 '14 at 17:27
2

Last time I did some research on this area (March 2013), I found this solution, which is not using JInput:

I've inspected how JFactory calls JSession but I haven't found a way to set the expiration when storing data.

Inside a system plugin, using the method onAfterRender()

    $cookie = session_get_cookie_params();

    $cookie['lifetime'] = DESIRED LIFETIME;

    session_set_cookie_params($cookie['lifetime'], $cookie['path'], $cookie['domain'], $cookie['secure'], true);

The only back-draw I found was that now there are two cookies with identical name and content. I haven't noticed any side effects.

Hope this helps.

Source: Joomla! General Development › Set front-end user session expiration

2

Try using something like the following:

$input  = JFactory::getApplication()->input;
$cookie = $input->cookie;
$post = $input->post;

$cookie->set('destinationcookie', $post->get('destination'), strtotime( '+90 days' ));
$destination =  isset($post->get('destination')) ? $post->get('destination') : $cookie->get($name = 'destinationcookie', $defaultValue = null);
  • Fatal error: Can't use method return value in write context in /path/to/templates/template/html/com_entrusters/itemform/default.php on line 28 – user2097091 May 11 '14 at 16:03
  • @user2097091 - I accidentally added an extra semi-colon. Updated the code. Can you try again now? – Lodder May 11 '14 at 16:12
  • You are using the variable $inputCookie which you've not defined - shouldn't this be $cookie? – MrWhite May 12 '14 at 0:33
  • @w3d - ahh yes my bad. Updated the code. Thanks for pointing that out – Lodder May 12 '14 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.