EllisLab text mark
Advanced Search
1 of 5
1
   
Plugin: Cookie Plus
Posted: 23 June 2008 01:31 PM   [ Ignore ]
Avatar
Joined: 2007-02-02
981 posts

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 23 June 2008 04:02 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-15
12649 posts

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!! wink

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) wink Wonder who did that first? wink

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

 
Posted: 23 June 2008 04:36 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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?  angry

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 23 June 2008 05:22 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-04-15
12649 posts
Laisvunas - 23 June 2008 08:36 PM

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?  angry

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

 

Just kidding grin Totally free of course wink grin wink

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

Speak to you soon.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin | Full List Of Add-Ons | About Me
——————————————————————————————
2.x Bug Tracker | Upgrade Errors

 
Posted: 26 June 2008 10:27 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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"
 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 31 July 2008 05:34 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-08-14
231 posts

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

 Signature 

Jim Pannell | Six Media Web Development | Copenhagen, Denmark
Twitter: @jimpannell

 
Posted: 31 July 2008 11:32 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 01 August 2008 06:44 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2004-10-10
265 posts

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} 
 Signature 

Twitter: @RobQuigley

 
Posted: 01 August 2008 06:49 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2004-10-10
265 posts

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

 Signature 

Twitter: @RobQuigley

 
Posted: 02 August 2008 06:16 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 02 August 2008 10:29 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2004-10-10
265 posts

This is just awesome! Thanks for revising smile 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! smile

 Signature 

Twitter: @RobQuigley

 
Posted: 02 August 2008 02:47 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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 
setCookienamevalueexpirespathdomainsecure {
    
var today = new Date();
    
today.setTimetoday.getTime() );
    if ( 
expires {
        expires 
expires 1000;
    
}
    
var expires_date = new Datetoday.getTime() + (expires) );
    
[removed] name+'='+escapevalue ) +
        ( ( 
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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 02 August 2008 08:14 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2004-10-10
265 posts

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

 Signature 

Twitter: @RobQuigley

 
Posted: 06 August 2008 07:24 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2007-07-20
58 posts

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.

 
Posted: 07 August 2008 01:55 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2007-02-02
981 posts

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.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

 
Posted: 07 August 2008 04:42 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2007-07-20
58 posts

Hi Laisvunas, thanks for your help.

I’ve created a simple template to test if I can retrieve the cookie by just using PHP. (I haven’t created an EE plugin in my life, so I don’t have the SQL query wrapped in one. I should try!)

But the test is meant to simply output the value of the cookie:

<p>From plugin{exp:cookie_plus:get name="user_in"}</p>
<
p>From php<?php echo $_COOKIE["user_in"]?></p

Using your plugin it works perfectly, but this is what I get from PHP in my browser:

NoticeUndefined indexuser_in in /home/.manet/cret/costaricaexotictravel.com/cretee/core/core.functions.php(635) : eval()'d code on line 27 

What could this be?

Thanks man!

 
1 of 5
1