We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question

Setting cookies when Perch is included

I've never used cookies before so this could simply be a beginner misunderstanding…

In trying to create an extremely basic test case I've discovered that as soon as I include Perch I cannot set cookies. For example, if I have a page called index.php which comprises of this:

<?php

    setcookie('username', 'alex', time()+600);
    echo $_COOKIE['username'];

?>

A cookie of "username: alex" is successfully created. If I delete the cookie then include Perch like this:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');?>

<?php

    setcookie('username', 'alex', time()+600);
    echo $_COOKIE['username'];

?>

The cookie fails to set.

Is there something I need to do differently when setting cookies while Perch is running on the page? I couldn't find any mention of cookies in the documentation.

Jay George

Jay George 2 points

  • 2 years ago

I think the problem is you are trying to output a cookie as soon as you've set one, once you set a cookie the page needs to reload before you are able to echo the cookie value.

If you open up your browser console, and look at the cookies I think it will be set.

Hi Dexter,

That wasn't the case actually. I eventually got it to work by changing the spacing slightly which I find completely bizarre.

This did NOT work:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');?>

<?php

    setcookie('username', 'alex', time()+600);
    echo $_COOKIE['username'];

?>

Whereas this DID work:

<?php

    if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');

    setcookie('username', 'alex', time()+600);
    echo $_COOKIE['username'];

?>

Do you have any idea why this would be? I can repeatedly replicate this so it's not a freak coincidence. I find it really odd, but it's definitely a thing.

Is this just a normal page on your site, using a master page and created within the Perch admin?

If so you shouldn't even need the runway check

No, it will be a unique page eventually that a link is directed to, to set a cookie (as a "no-JS" fallback).

The code I posted is literally the only code on the page at the moment, which is why I'm confused by the spacing issue.

Hmm I don't know then, I can't replicate it here. Maybe support can shed some light

I have a feeling it may be something to do with "outputting before sending headers"

Functions that send/modify HTTP headers must be invoked before any output is made, otherwise the call fails: Some functions modifying the HTTP header are: header / header_remove session_start / session_regenerate_id setcookie / setrawcookie

Source

This would also make sense because the first example I pasted that was failing technically has a line (that's not in PHP) in between the two arguments

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, watch the whitespace you're sending.