Hi fogcity,
Check two things:
1) in your example in the closing tag “:get” is missing. That is, your code should be this:
{exp:cookie_plus:set name="subscribed" value="1" seconds="16000000" method="script"}
{exp:cookie_plus:get name="subscribed"}
{if cookie != "1" }
{!-- show the subscription form --}
{/if}
{/exp:cookie_plus:get}
If after making this correction the code does still not work as expected, then
2) go CP Home > Admin > System Preferences > Cookie Settings and change the value of “Cookie Prefix” field into “exp_”.
This code with such value of “Cookie Prefix” setting works correcly on my EE installation.
Wondering if you or someone might be able tell me if I can set a cookie using a dropdown select box.
No, you can’t use Cookie Plus plugin to set a cookie using dropdown select box. Using Cookie Plus plugin a cookie can be set only when either page load or page unload event happens.
To set a cookie using a dropdown select box you will use onchange event and simple client-side javascript.
The code would be approximately as follows:
<html>
<head>
<script>
var my_selectbox=document.getElementById('my_selectbox');
my_selectbox.onchange=function() {
if (this.options.selectedIndex==0) {
document. cookie =
'my_cookie=testcookie1; expires=Thu, 6 Aug 2009 20:47:11 UTC; path=/';
}
else if (this.options.selectedIndex==1) {
document. cookie =
'my_cookie=testcookie2; expires=Thu, 6 Aug 2009 20:47:11 UTC; path=/';
}
else if (this.options.selectedIndex==2) {
document. cookie =
'my_cookie=testcookie3; expires=Thu, 6 Aug 2009 20:47:11 UTC; path=/';
}
}
</script>
</head>
<body>
<select id="my_selectbox">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</body>
</html>
There’s an extra underscore there. So I made this change:And that works. Again, my set uses the name “subscribed” but my get needs the name “_subscribed”.{exp:cookie_plus:get name="_subscribed"} {if cookie != "1" } {!-- show the subscription form --} {/if} {/exp:cookie_plus}
I just encountered the same issue. Thanks for your solution.
Hi Nico,
Check the value of CP > Admin > System Preferences > Cookie Settings > Cookie Prefix.
If the value of “Cookie Prefix” is “exp_” then plugin should work OK.
If “Cookie Prefix” value is “exp” or no value is set, then you should either set the value to “exp_” or remove one underscore from the line you quoted.
Cool plugin! I’m trying to redirect to a page if cookie doesn’t exist, but it doesn’t seem the {redirect} function works inside the if statement. Do you know why this would be? here is my code:
{exp:cookie_plus:get name="confirm"}
{if cookie!="yes"}
{redirect="confirmation"}
{/if}
{/exp:cookie_plus:get}
Hi,
The variable {redirect} performs PHP redirect wchich may not work in some circumstances. If this happens, try to use Redirect To plugin. This plugin allows you to perform both PHP and Javascript redirect.
Hi, The variable {redirect} performs PHP redirect wchich may not work in some circumstances. If this happens, try to use Redirect To plugin. This plugin allows you to perform both PHP and Javascript redirect.
The redirect plugin worked, thanks for suggesting it.
Hi Laisvunas. Another great plugin. Thank you!
I have a question about why I am seeing the value of a cookie rendered on a page, when I am using a conditional statement.
Here is my current code
{exp:cookie_plus:get name="legacytext"}
{if cookie=="text"}
{if:else}
{if segment_1 == ""}
<ul id="main-image">
{exp:weblog:entries weblog="homepage" status="images" limit="3" orderby="random" cache="yes" refresh="10" disable="categories|member_data|pagination|trackbacks"}
<li>{image}</li>
{/exp:weblog:entries}
</ul><!--end main-image-->
{if:elseif segment_1 == "search"}
<ul id="section-banner-image">
<li>/images/protecting-lands-banner.jpg</li>
</ul>
{if:elseif segment_1 == "board-only"}
<ul id="section-banner-image">
<li>/images/how-you-can-help-banner.jpg</li>
</ul>
{if:elseif segment_1 == "site"}
<ul id="section-banner-image">
<li>/images/protecting-lands-banner.jpg</li>
</ul>
{if:else}
<ul id="section-banner-image">
<li>/images/{segment_1}-banner.jpg</li>
</ul>
{/if}
{/if}
{/exp:cookie_plus:get}
At first I figured it was probably because I had another conditional nested. But, I removed the nested conditional and still see “text” rendered on my page. What I am trying to do is say, if the cookie is there, do nothing, if it is is not there, do the rest of the stuff.
This is the code I am using to set the cookie.
{exp:cookie_plus:set name="legacytext" value="text" seconds="2628000"}
Thanks!
UPDATE - I figured out what I did wrong. I actually called the cookie_plus plugin twice on the same page. And, I simply did not close the first call. Everything is working great now. 😊 Thanks!
Thanks for this plugin. I am having some trouble getting it to work, though.
I have products that are child categories of a parent. These products can be in multiple child categories. When navigating to a product I want the subnav to reflect the siblings of the whichever category the visitor was viewing before clicking on the product.
Is a cookie the best way to go?
Here I am setting the cookie in the products/index (rendered in category view):
{exp:weblog:category_heading weblog="products"}
{embed="embeds/product_nav" product_category="{category_id}"}
{exp:cookie_plus:set name="sectioncookie" value="{category_id}" seconds="0" method="script"}
{/exp:weblog:category_heading}
And also here in the products/list (also rendered in category view, but after someone has selected a subcategory):
{exp:weblog:category_heading weblog="products"}
{exp:child_categories sibling="{category_id}" child_categories_sort_by="custom" show_empty="yes"}
{parent_category_start}
{embed="embeds/product_nav" product_category="{parent_category_id}"}
{exp:cookie_plus:set name="sectioncookie" value="{parent_category_id}" seconds="0" method="script"}
{parent_category_end}
{/exp:child_categories}
And here is where I try to get the cookie when viewing a single product at the url product/detail (not in category view):
{exp:cookie_plus:get name="sectioncookie" parse="inward"}
{exp:weblog:categories weblog="products" style="linear" show="{cookie}" parent_only="no" show_empty="yes"}
<a href="#">{category_name}</a>
{total_results}
{/exp:weblog:categories}
{/exp:cookie_plus:get}
Note, this isn’t the final menu code, but I can’t get it to just list out the categories. Using the developer toolbar in Firefox I can see that the cookie is set so the problem is in the get or the parse.
Thanks!
SUPER plugin, thank you - just what I was looking for after having played with the original cookie.
Question though, has this extra underscore thing ever really been sorted out? I have tried several times to edit the prefix to the place you suggested in the cp, but I can’t get it to save, the value just disappears after I hit save.
Meanwhile, prepending an “_” to my cookie name works, but isn’t this a bit of a hack? Seems odd.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.