1

We have a Laravel app that sets a cookie:

public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
    list($path, $domain) = $this->getPathAndDomain($path, $domain);

    $time = ($minutes == 0) ? 0 : time() + ($minutes * 60);

    return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly);
}

The values passed are as follows:

array (size=7)
    0 => string 'imph_auth' (length=9)
    1 => string 'Russ Back' (length=9)
    2 => int 2628000
    3 => null
    4 => null
    5 => boolean false
    6 => boolean true

I've tried to retrieve that cookie in a Craft template as follows:

craft.request.getCookie('imph_auth')

However I get null when I know that it's been set (I can browse to a Laravel template on the same domain and get the cookie).

I note the JavaScript warning in the Craft docs but this is set in PHP, however I can see the getCookies() method of the CookieCollection class runs each cookie through craft()->security->validateData() and fails validation.

Is there any way I can get around this without resorting to my own PHP in the template or a custom plugin?

1

Is there any way I can get around this without resorting to my own PHP in the template or a custom plugin?

You can't have PHP is a Twig template, but I think you'd need a real simple plugin for this.

It would just have an init() method that did:

craft()->request->enableCookieValidation = false;

Just be aware that's going to make it much easier for people to potentially tamper with cookie values.

| improve this answer | |
  • 1
    Thanks, Brad. I opted instead for a simple plugin with a Twig extension that lets me get a cookie for a specific key, so everything else remains secure. – Russ Back Feb 20 '15 at 6:45
  • @RussBack could you mention the name of the plugin you're using? – nateclonch Dec 2 '17 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.