MB34
MB34

Reputation: 4404

PHP and Javascript cookie handling - different between browsers

We have a site that uses ExpressionEngine as the CMS. We also use Magento's cart in the e-commerce part of our site. I'm having difficulties with cookies and making them accessible in different areas.

We use a cookie for search selections so when the user returns to our schedule page it remembers the last selection they saved, up to 2 days ago.

I also allow them to use the URL to set them using the URLs shown in the comment below. The cookie is used to filter items on the schedule based on the last or last 2 segments.

//*****************************************************************************
// This is from an Expressionengine base template to set the cookie before the
// header is sent.
// These are ExpressionEngine tags that evaluate the path segments
// the URL is www.mysite/schedule/CMOL or www.mysite/schedule/CM/TX
//*****************************************************************************
{if segment_1 == "schedule" }
  {if segment_2 != ""}
<?php  
    $state = '{segment_3}'; // Could be empty
    $pro = '';
    //*******************************************************
    // clear the searchItems cookie
    // this doesn't actually clear the cookie for some reason.
    //*******************************************************
    setcookie("searchItems", "", time()-7200, '/schedule/');
    {if segment_2 == 'CMOL'}
        $state = 'OL';
        $pro = '21_FUN,22_HAN,23_MEA,24_FSR,25_ADM';
    {/if}
    {if segment_2 == "CM"}
        $pro = '21_FUN,22_HAN,23_MEA,24_FSR,25_ADM';
    {/if}
    $cookie = '2013][][0][25]['.$state.']['.$pro.'][true';

    //*******************************************************
    // Now reset it to the pro and/or state selection expires 
    // 2 days from now.
    //*******************************************************
    setcookie("searchItems", $cookie, time()+172800, '/schedule/');
?>  
  {/if}

Now, on the schedule page, I have some javascript to set the cookie in the window.onUnload to set the cookie for their search preferences. The code and value used to set the cookie in javascript looks like this:

// Values
// c_name  = "searchItems"
// c_value = "2013%5D%5B%5D%5B0%5D%5B25%5D%5BOL%5D%5B16_FUN%2C17_HAN%2C18_MEA%2C19_FSR%2C20_ADM%5D%5Btrue; path=/schedule/;;  expires=Sat, 26 Jan 2013 17:09:37 GMT"

//*******************************************************   
// Function to set the search preference cookie
//*******************************************************   
function setCookie(c_name,value,exdays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + '; path=/schedule/;' + ((exdays==null) ? "" : "");
    if(remember_params_flag) {
        c_value += " expires="+exdate.toUTCString();
    }
    document.cookie=c_name + "=" + c_value+';';
}

What am I doing wrong as far as cookies go in order to make them work across all browsers? They do not have to share this information between browsers, only browser sessions.

BTW, due to the inconsistencies between the SESSION handling in Magento and ExpressionEngine, I can not accept a PHP SESSION option.

Upvotes: 0

Views: 447

Answers (2)

MB34
MB34

Reputation: 4404

I went through and double checked all the places I was writing and reading the cookies. I made sure they were all written in the same manner, using the domain and making sure that the expiration was all set the the same value.

Everything looks like it's working now.

Upvotes: 0

Jordan Kasper
Jordan Kasper

Reputation: 13273

A couple things, I think you may want to use the onbeforeunload event, but it is not supported everywhere, so you may need to do some detection. Second, note that there are ways that neither event will get fired (crashes), but you probably don't need to worry about them for your use case. As for your method, it looks ok, but you never know. I would be sure to test the setCookie method outside of an onunload event to make sure it works as-is, then you can start looking into when the event fires across different browsers.

Check out these links as well for info on setting cookies:

Upvotes: 1

Related Questions