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

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

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:

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

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!

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

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;

?>
       
Nathan Pitman's avatar
Nathan Pitman
531 posts
16 years ago
Nathan Pitman's avatar Nathan Pitman

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

       
cicconedesign's avatar
cicconedesign
6 posts
16 years ago
cicconedesign's avatar cicconedesign

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!

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas
I am using the retrieve info within an include that is embedded within the page.

I’m not sure I understand correctly what you mean here. Can you post the code?

I tried the code you posted and found that included in usual template it works correctly.

       
cicconedesign's avatar
cicconedesign
6 posts
16 years ago
cicconedesign's avatar cicconedesign

Ok here goes. 😊

My current website is http://www.reeb.com/index.php/index/ which shows product info for EAST location.

I am being asked to create a second web page that will highlight WEST coast product info http://www.reeb.com/index.php/indexWest/

My header and navigation along with other elements on the page are created using includes/embeds http://expressionengine.com/docs/templates/embedding_templates.html

I want the REEB logo and the HOME button to link to the home page of EAST when the user has selected EAST and WEST when the user has selected WEST.

My branding include code:

<div id="branding">
<a href="http://www.reeb.com/index.php/indexWest/%22name=%22top">/images/branding/reebLogo.jpg</a>
{if:else}
<a href="http://www.reeb.com/index.php/index/">/images/branding/reebLogo.jpg</a>
{/if}
{/exp:cookie_plus:get} 
</div>

My page code:

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

{assign_variable:pageTitle="REEB | Welcome to REEB"}
{assign_variable:pageHeader="Home"}
{assign_variable:pageID="home"}
{assign_variable:css_style="home"}

{assign_variable:my_template_group="weblog"}

{embed="includes/doc_type"}

<head>

{embed="includes/metadata"}

<title>{pageTitle}</title>

{embed="styles/{css_style}"}
{embed="includes/javascript"}

</head>
<body id="{pageID}">
{embed="includes/login"}
{embed="includes/select_location"}
<div id="container"> 
{embed="includes/branding"}[color=red]<!-- branding included here -->[/color]
  <div id="navSubWrapper"> {embed="includes/subNav"} </div>
  <div id="navMainWrapper"> {embed="includes/mainNav"} </div>
  <div id="content">
    <div id="contentMain">
      <div id="highlightWrapper">
        <div id="highlightContent">
          <div id="left"><!-- Highlight left image BEGIN -->
              {exp:gallery:entries gallery="highlight_banner_3" category="39" orderby="random" limit="1" dynamic="off"}
                          <a href="http://{custom_field_one}">{image_url}</a>
              {/exp:gallery:entries}
          </div><!-- Highlight END -->
          <div id="right"><!-- Highlight right image BEGIN -->
               {exp:gallery:entries gallery="highlight_banner_2" category="11" orderby="random" limit="1" dynamic="off"}
                           <a href="http://{custom_field_one}">{image_url}</a>
               {/exp:gallery:entries}
          </div><!-- Highlight END -->
        </div>
      </div>
      <div id="pageTitle">
      </div>
      <div id="mainTextWrapper">

      <div id="newProducts"> 
      <h2>New Products</h2>
       {exp:weblog:entries weblog="weblog" category="27" orderby="date" sort="desc" limit="5" disable="trackbacks"}
       
       <div class="productHighlightImage">{image}</div>
       <ul>
            <li> <a href="http://{permalink="><span class="yellow">{title}</span></a></li>
       </ul>
             {summary}
       <a href="http://{permalink=">Read More</a><br><br>
       <div class="hr" align="center">/images/layout/hr.jpg</div>  
       {/exp:weblog:entries} 
       </div>
  
      <div id="spotlight">
       <h2>Spotlight</h2>          
       {exp:weblog:entries weblog="weblog" category="25" orderby="date" sort="desc" limit="3" disable="trackbacks"}<br>
       {title}<br>
       {image}    
       {summary}
       <h4>{comment_total} comments // {entry_date format="%F %d, %Y"}</h4>
       <a href="http://{permalink=">Read More</a>
       <div class="hr" align="center">/images/layout/hr.jpg</div>
       {/exp:weblog:entries}
       </div>
       
       <div id="highlightColumn"> <!-- Highlight Column BEGIN -->
        
        {embed="includes/product_categories"}
        <div id="newsHighlight">
    <h2>Latest News</h2>
         <ul>         
             {exp:weblog:entries weblog="weblog" orderby="date" sort="desc" limit="5" disable="trackbacks" dynamic="off"}
                     <li><a href="http://{permalink=">{title}</a><br><h4>// {entry_date format="%F %d, %Y"}</h4></li>
             {/exp:weblog:entries} 
         </ul>
    <a href="http://www.reeb.com/index.php/weblog/">View All</a>
</div>
        {embed="includes/newest_media"}
        
        </div><!-- Highlight Column END-->
    </div>
</div>

{embed="includes/manufacturer_scrolling_logos"}
{embed="includes/site_info"}

</div>

{embed="includes/footer_javascript"}

</body>
</html>

Like I said i’m new to this stuff, maybe this is not the best way to achieve this??

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

Hi cicconedesign,

It is irrelevant that the code that sets the cookie is on the main template and the code that reads the cookie is on embedded template. Both setting cookie and reading it should work. That is, the first time you load the page cookie will be set, the second time and so on cookie will be both set and retrieved. If it does not work this way for you I have no idea what might be the cause.

maybe this is not the best way to achieve this??

Really, it is wrong way to achieve functionality you need, because the right link will be displayed only starting from the second load of the page. You would better use URL segment variables. The code would be as follows:

{if segment_1=="indexWest"}
<a href="http://www.reeb.com/index.php/indexWest/">/images/branding/reebLogo.jpg</a>
{if:else}
<a href="http://www.reeb.com/index.php/index/">/images/branding/reebLogo.jpg</a>
{/if}
       
cicconedesign's avatar
cicconedesign
6 posts
16 years ago
cicconedesign's avatar cicconedesign

Thanks Laisvunas!

I used the URL Segment Variables as you suggested. It works great.

Thanks again. 😊

       
mmcclung's avatar
mmcclung
172 posts
16 years ago
mmcclung's avatar mmcclung

w00t!

thank you for this. solved a problem in testing for presence of a cookie inside a {body} tag with {exp:allow_eecode}

       
fogcity's avatar
fogcity
260 posts
16 years ago
fogcity's avatar fogcity

BUG REPORT:

Cookies saved on my browsers (Safari 3.2.1 mac, Firefox 3.0.5 mac) are having an underscore added to the cookie name.

I’m saving a cookie called “subscribed” but it’s stored as “subscribed” (EE’s cookies start with exp for example exp_last_visit whereas this plugin’s cookies start with exp__ for example exp__subscribed). I’m using this plugin’s “script” method.

The workaround seems to be to save it as “subscribed” but refer to it as “_subscribed” when retrieving it.

Note that I haven’t tried this on Windows.

Anyone else seen this?

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

Hi fogcity,

Why do you think this should be considered a bug?

The plugin can both set a cookie and read the cookie it has set earlier. It was not supposed to read cookies set by EE or vice versa.

       
fogcity's avatar
fogcity
260 posts
16 years ago
fogcity's avatar fogcity

hi there,

it should be a bug because it doesn’t work as expected or as documented. in order to read a cookie previously created by this plugin, an underscore must be added to the name of the cookie or this plugin won’t find it. at least in the two browsers i mentioned. sounds like a bug to me.

       
Laisvunas's avatar
Laisvunas
879 posts
16 years ago
Laisvunas's avatar Laisvunas
in order to read a cookie previously created _by this plugin_, an underscore must be added to the name of the cookie or this plugin won’t find it.

Do you mean the plugin cannot read a cookie it has set earlier? That would really be a bug.

Can you describe an example of such behaviour, so that I could reproduce it?

       
fogcity's avatar
fogcity
260 posts
16 years ago
fogcity's avatar fogcity

Yes, this plugin cannot read a cookie it has set earlier. I thought that was clear in my original post.

When a user subscribes, I save a cookie called “subscribed”:

{exp:cookie_plus:set name="subscribed" value="1" seconds="16000000" method="script"}

When I try to retrieve it:

{exp:cookie_plus:get name="subscribed"}
    {if cookie != "1" }
        {!-- show the subscription form --}
    {/if}
{/exp:cookie_plus}

It doesn’t work. The subscription form is always shown.

So I checked my cookies, and I see a cookie named “exp__subscribed”. There’s an extra underscore there. So I made this change:

{exp:cookie_plus:get name="_subscribed"}
    {if cookie != "1" }
        {!-- show the subscription form --}
    {/if}
{/exp:cookie_plus}

And that works. Again, my set uses the name “subscribed” but my get needs the name “_subscribed”.

The above behavior is the same on Safari 3.2.1 mac, Firefox 3.0.5 mac.

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

Well, I will investigate this issue in following days.

       
1 2 3 4 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.