1 of 2
1
Plugin: Cookie Plus
Posted: 23 June 2008 10:31 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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.

File Attachments
pi.cookie_plus_v1.2.zip  (File Size: 3KB - Downloads: 114)
 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 23 June 2008 01:02 PM   [ Ignore ]   [ # 1 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006

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 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 23 June 2008 01:36 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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 plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 23 June 2008 02:22 PM   [ Ignore ]   [ # 3 ]  
Research Scientist
Avatar
RankRankRankRankRankRank
Total Posts:  6405
Joined  04-15-2006
Laisvunas - 23 June 2008 01: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 

Full List Of Plugins Here!! (16)
 
Retrieve Statuses
Maximum Posts Reached
Neat Link
Redirect
Fetch URI

Profile
 
 
Posted: 26 June 2008 07:27 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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 plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 31 July 2008 02:34 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  102
Joined  08-14-2007

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

Profile
 
 
Posted: 31 July 2008 08:32 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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 plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 01 August 2008 03:44 PM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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}

Profile
 
 
Posted: 01 August 2008 03:49 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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

Profile
 
 
Posted: 02 August 2008 03:16 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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 plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 02 August 2008 07:29 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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

Profile
 
 
Posted: 02 August 2008 11:47 AM   [ Ignore ]   [ # 11 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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.

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 02 August 2008 05:14 PM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  420
Joined  10-10-2004

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

Profile
 
 
Posted: 06 August 2008 04:24 PM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  24
Joined  07-20-2007

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.

Profile
 
 
Posted: 06 August 2008 10:55 PM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

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 plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 07 August 2008 01:42 PM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  24
Joined  07-20-2007

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:

Notice: Undefined index: user_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!

Profile
 
 
Posted: 07 August 2008 10:45 PM   [ Ignore ]   [ # 16 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  271
Joined  02-02-2007

Hi arteylogica,

Try the code which uses not only pure vanilla PHP, but also functions in-built into ExpressionEngine:

<?php

global $IN;

$my_cookie_value = $IN->GBL('user_in', 'COOKIE');

echo
$my_cookie_value;

?>

 Signature 

Full list of plugins here

Child Categories
Browser Sniff
Category Id
Entries List

Profile
 
 
Posted: 31 August 2008 02:36 PM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  549
Joined  10-14-2005

What an awesome little plug-in. Nice work. Just set this up to set a cookie containing a users details that were entered via ‘Freeform (Solspace)’ so that I can pre-fill the form on return visits. Works a treat. smile

 Signature 

Nathan Pitman

Nine Four Ltd - a member of the EEPro Network

Profile
 
 
Posted: 03 September 2008 12:40 PM   [ Ignore ]   [ # 18 ]  
Summer Student
Avatar
Total Posts:  3
Joined  08-03-2007

Hi all,

this is my first attempt at using cookies and thought I would give this plug-in a try. Potentially it sounds great but i’m having no luck retrieving a cookies value.

i’m using this code to set my cookie:

{exp:cookie_plus:set name="mylocation" value="west" seconds="0"}

setting it to “0” because I want a session cookie as described.

to retrieve i’m using:

{exp:cookie_plus:get name="mylocation"}
{if cookie
=="west"}
   item one
{if
:else}
   item two
{
/if}
{
/exp:cookie_plus:get}

I am using the retrieve info within an include that is embedded within the page.

Cheers in advance!

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64493 Total Logged-in Users: 55
Total Topics: 81040 Total Anonymous Users: 29
Total Replies: 436106 Total Guests: 252
Total Posts: 517146    
Members ( View Memberlist )