We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Cookie Plus

Development and Programming

Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

The attached add-on files may be out of date. Please visit Devot-ee for the latest versions.

Hi,

I have rewritten Eric Snyder’s Cookies plugin. The result is Cookie Plus plugin.

I was dissatisfied with two aspects of Cookies plugin:

1) using it you should place the code for setting cookie on top of the page and cookie must be set before any output started;

2) it is not possible to use retrieved cookie as parameter of other tags.

Cookie Plus plugin has no such constrictions and retains all functionality of Cookies plugin.

Usage:

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600"}

Sets a cookie named mycookie with the value “hellow!”. The cookie will last for 1 hour (3600 seconds). Set the seconds to zero to set a session cookie.

By default cookie is set using PHP function. This means that {exp:cookie_plus:set} tag must be on top of the page and do its job before any other output. If you need to set a cookie after output started, you may use parameter “method”. In case parameter “method” has a value “script” as here

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script"}

the cookie will be set using client-side javascript.

Using client-side javascript cookie by default will be set during page load event. Also cookie will be set during page load event if “event” parameter is set to “load”. To set cookie during page unload event, parameter “event” should have the value “unload”:

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script" event="unload"}

To retrieve a cookie:

{exp:cookie_plus:get name="mycookie"}

If the cookie “mycookie” exists then it will return the value. If the cookie does not exist then it will return nothing.

Alternatively for retrieving a cookie you may use {cookie} variable placed between {exp:cookie_plus:get} and {/exp:cookie_plus:get} tag pair.

Variable {cookie} within {exp:cookie_plus:get} and {/exp:cookie_plus:get} tag pair is more powerful because you can use its output as parameter of other tag. For such use you must add parse=”inward” parameter to {exp:cookie_plus:get} tag.

For example, code as this will work

{exp:cookie_plus:get name="mycookie" parse="inward"}
{exp:weblog:entries weblog="{cookie}" show="1|3|5"}
Some code
{/exp:weblog:entries}
{/exp:cookie_plus:get}

Also you can use retrieved cookie value in conditionals:

{exp:cookie_plus:get name="mycookie"}
{if  cookie=="news"}
Some code
{/if}
{/exp:cookie_plus:get}

Changelog:

-1.2 // Added possibility to use retrieved cookie value in conditionals. -1.1 // Parameter “event” added. -1.0 // First release.

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

Sounds like a great little plugin. Will download this and have a play. I actually made one like this myself some time back but never released it as I didn’t have the time to neaten it all up as other things took precedence as they always tend to do!! 😉

Thanks for this one. I’m sure it will be very helpful to a lot of people.

Best wishes,

Mark

P.S. Just noticed that your signature has the same “Full list of plugins here” as mine does (minus the exclamation marks of course) 😉 Wonder who did that first? 😉

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi Mark,

Nice that you like the idea of this plugin and hope that you will find it useful.

In writing the signature “Full list of plugins here” you were first, of course. I hope you will not charge any copyright fee, or will you? 😡

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
17 years ago
Mark Bowen's avatar Mark Bowen
In writing the signature “Full list of plugins here” you were first, of course. I hope you will not charge any copyright fee, or will you? 😡

You can have it at a reduced rate of £10,000,000 😉

Just kidding 😊 Totally free of course 😉 😊 😉

Thanks again for releasing this one. Surely will be of great use to a lot of people.

Speak to you soon.

Best wishes,

Mark

       
Laisvunas's avatar
Laisvunas
879 posts
17 years ago
Laisvunas's avatar Laisvunas

Hi,

I have updated the plugin Cookie Plus. You can download version 1.1 using the link in previous post.

In this release a new parameter - “event” was added. This parameter is used together with “method” parameter. If “method” parameter is set to “script”, then, using “event” parameter you can define when cookie will be set. If “event” parameter is not set or has a value “load”, then javascript will set cookie during page load event. If event parameter has a value “unload”, then javascript will set cookie during page unload event.

An example:

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script" event="unload"}
       
Jim Pannell's avatar
Jim Pannell
187 posts
17 years ago
Jim Pannell's avatar Jim Pannell

Hi Laisvunas

Your plugin looks really interesting - thanks. Would it be possible for you to give the community some example uses for the script? For example, I’m currently looking at ways to implement a very simple shopping cart for a site. Could Cookie Plus do this and if so how?

Many thanks

Jim

       
Laisvunas's avatar
Laisvunas
879 posts
about 17 years ago
Laisvunas's avatar Laisvunas

Hi Jim,

Having no experience in implementing shopping carts I cannot say if Cookie Plus plugin could somehow be useful for such purpose.

I use Cookie Plus plugin only when it is really complicated to pass some custom data from one page to another. One such case would be if you need to pass some custom date from a page containing search form to search results_page or no_results_page. In this case you cannot pass custom data using URL segments or PHP $_POST array (see here). So cookies is right solution for such case.

       
Rob Quigley's avatar
Rob Quigley
236 posts
about 17 years ago
Rob Quigley's avatar Rob Quigley

The weblog code example you provide (repeated below) works for me no problem but I wanted to use a simple conditional like the second code example I provide below.

{exp:cookie_plus:get name="mycookie" parse="inward"}
{exp:weblog:entries weblog="{cookie}" show="1|3|5"}
Some code
{/exp:weblog:entries}
{/exp:cookie_plus:get}

This below doesn’t work but something like that?

{exp:cookie_plus:set name="mycookie" value="news" seconds="3600"} 

{exp:cookie_plus:get name="mycookie" parse="inward"}
{if  {cookie} =="news" }
Some code
{/if}
{/exp:cookie_plus:get}
       
Rob Quigley's avatar
Rob Quigley
236 posts
about 17 years ago
Rob Quigley's avatar Rob Quigley

And one other question - if you have time and inclination - would you mind showing an example of how someone might set a cookie using some javascript and a text link or some other mechanism to trigger setting the cookie in the page while using your plugin?

That may be a little bit much to flush out for an example but if one is handy that would be great. 😊

       
Laisvunas's avatar
Laisvunas
879 posts
about 17 years ago
Laisvunas's avatar Laisvunas

Hi Rob,

I just released version 1.2 of Cookie Plus plugin. Now you can use retrieved cookie value in conditionals. That is, the code as this will work:

{exp:cookie_plus:get name="mycookie"}
{if  cookie=="news"}
Some code
{/if}
{/exp:cookie_plus:get}

Concerning your question

would you mind showing an example of how someone might set a cookie using some javascript and a text link or some other mechanism to trigger setting the cookie in the page while using your plugin?

I would say that using Cookie Plus plugin the cookie can be set either during page load or unload. If there is no parameter “event”, then the cookie will be set during page load event, as here:

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script"}

If there is parameter “event” and it has the value “unload”, then the cookie will be set during page unload event, as here:

{exp:cookie_plus:set name="mycookie" value="hellow!" seconds="3600" method="script" event="unload"}

There is no need to enter any additional javascript or text link.

       
Rob Quigley's avatar
Rob Quigley
236 posts
about 17 years ago
Rob Quigley's avatar Rob Quigley

This is just awesome! Thanks for revising 😊 I’m really looking forward to using this.

Sorry you may have just stated this, and I’m a little dense on this subject, but if I was looking to only set a cookie if a text link was clicked after the page loaded, can I set that cookie somehow using your plugin?

Or does your plugin just set it when the page initially loads or alternately when the page finishes? Sorry, I think I’m having difficulty understanding the concept and terminology of values of “unload” and “event.” That’s why if it was possible, I was hoping to see a basic visual example of code potentially used to set for an on click event or something similar.

My apology and thank you for your patience! 😊

       
Laisvunas's avatar
Laisvunas
879 posts
about 17 years ago
Laisvunas's avatar Laisvunas
Or does your plugin just set cookie when the page initially loads or alternately when the page finishes?

Correct.

if I was looking to only set a cookie if a text link was clicked after the page loaded, can I set that cookie somehow using your plugin?

To achieve this you should use not plugin but a simple javascript function. Try do as follows. First insert this into <head> part of your page:

<script>
function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) {
        expires = expires * 1000;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    [removed] = name+'='+escape( value ) +
        ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
        ( ( path ) ? ';path=' + path : '' ) +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ( ( secure ) ? ';secure' : '' );
}
</script>

Then insert this into appropriate place inside <body> part of your page:

<a onclick="setCookie('mycookie', 'news', 3600, '/'); return false;">My link</a>

Hope this helps.

       
Rob Quigley's avatar
Rob Quigley
236 posts
about 17 years ago
Rob Quigley's avatar Rob Quigley

Thanks! I was confused but I’ve got it down now. This plugin is really going to be handy for me.

       
arteylogica's avatar
arteylogica
37 posts
about 17 years ago
arteylogica's avatar arteylogica

Hi, your plugin is excellent!

I just have one question: How can I retrieve the value of the cookie directly from PHP?

I’m using the cookie_plus plugin to store a value that I later need to retrieve for a query I’m making to a database. So I need the value stored in a php variable.

Something like

<?php

$my_cookie_value = (I don't know how to retrieve it);

?>

Thanks so much!

G.

       
Laisvunas's avatar
Laisvunas
879 posts
about 17 years ago
Laisvunas's avatar Laisvunas

Hi arteylogica,

To retrieve cookie value using PHP you can use the code as this:

<?php

$my_cookie_value = $_COOKIE["my_cookie_name"];

?>

Using PHP in templates is not secure, so I would advice to you to wrap your database query into a plugin and use that plugin together with Cookie Plus plugin. Suppose your plugin would be named “Arteylogica DB query” and had parameter “cookie” (or some other parameter which should get its value from cookie). Then you could write the code as this:

{exp:cookie_plus:get name="mycookie" parse="inward"}
{exp:arteylogica_db_query cookie="{cookie}"}
Some code
{/exp:arteylogica_db_query}
{/exp:cookie_plus:get}

Hope that helps.

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.