Magento – Cookie Value Not Set

cookie

I'm trying to conditionally set a cookie IF a certain query string parameter exists. My Class is an observer. The config.xml code:

<frontend>
    <events>
        <controller_action_predispatch>
            <observers>
                <my_module>
                    <class>my_module/observer</class>
                    <method>setCookie</method>
                </my_module>
            </observers>
        </controller_action_predispatch>
    </events>
</frontend>

The observer class (app/code/local/My/Module/Model/Observer.php):

class My_Module_Model_Observer
{
    public $_defaultParam = null;

    public function setCookie(Varien_Event_Observer $observer = null, $setCookie = true)
    {
        $myCookie = 'source';
        $myCookieVal = $this->getQueryString($myCookie);
        $myCookiePeriod = (int) 86400 * 30;
        $myCookieEnabled = Mage::getStoreConfig('my_module/my_group/source_cookie', Mage::app()->getStore());
        if ($setCookie) {
            if ($myCookieEnabled == 1 && $myCookieVal) {
                $doCookie = $this->setCookie($myCookie, $myCookieVal, $myCookiePeriod, false);
            } else {
                if ($this->getCookie($myCookie)) {
                    $doCookie = $this->delCookie($myCookie);
                }
            }
        } else {
            $doCookie = $this->getCookie($myCookie, true);
        }
        return $doCookie;
    }

    public function getQueryString($param = null)
    {
        $queryParam = Mage::app()->getRequest()->getParam($param, 'notset');
        $paramValue = $queryParam ? (string) $queryParam : $this->_defaultParam;
        var_dump('ParamValue: ' . $paramValue);
        return $paramValue;
    }

    public function setCookie($cookieName = null, $cookieVal = null, $cookiePeriod = 3600, $setEncode = true)
    {
        $model = Mage::getModel('core/cookie');
        if ($setEncode) {
            $cookieVal = base64_encode($cookieVal);
        }
        $curStore = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
        $urlPath = parse_url($curStore);
        $cookiePath = rtrim(str_replace('index.php', '', $urlPath['path']), '/');
        var_dump('CookieVal: ' . $cookieVal);
        $theCookie = $model->set($cookieName, $cookieVal, $cookiePeriod, $cookiePath);
        return $theCookie;
    }
}

The query string looks like this:

mydomain.com?some=rubbish&then=what&we=really&want=this&source=my

You will notice in the method getQueryString, I'm var_dump'ing the variable $paramValue. It' spits out string(14) "ParamValue: my".

Also, in the setCookie method, the var_dump spits out string(13) "CookieVal: my".

HOWEVER, the cookie is set with the value notset! That means the $param is null, yes? But the var_dumps tell me otherwise. Why is the correct value not set in the cookie?

I guess I've been staring at this too long and need some eyes; could be my own stupidity! Thanks for taking a look.

~ Edit ~

I also tried to loop through all the query string params by changing the getQueryString method to the following:

public function getQueryString($param = null)
{
    $queryParams = Mage::app()->getRequest()->getParams();
    foreach ($queryParams as $pid => $p) {
        $paramValue = ($pid == 'source') ? $p : $this->_defaultParam;
    }
    var_dump('ParamValue: ' . $paramValue);
    return $paramValue;
}

But the result is the same.

Best Answer

Solved; thanks to @vinai for pointing out the following:

Using var_dump will trigger the headers to be sent, so trying to set a cookie after that point will obviously fail.

This is not explicitly defined in the PHP manual, but if you think about it, it makes sense! They do say ...as with anything that outputs its result directly to the browser...

Related Topic