CodeIgniter Forums

Full Version: Is CI setting new cookies to expire yesterday?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm having problems setting a cookie:

PHP Code:
$this->input->set_cookie('XDEBUG_PROFILE'$set); 

The cookie immediately expires the moment it is created. I've chased the problem down to this:

In 3.1.7, in the /system/core/input.php file at line 399, we find this code for setting the expiration of a cookie:

PHP Code:
        if ( ! is_numeric($expire))
        {
            $expire time() - 86500;
        }
        else
        
{
            $expire = ($expire 0) ? time() + $expire 0;
        

If you don't explicitly pass an expiration time, it defaults to 24 hours ago. Which means cookies will immediately disappear.

Is this a bug? Shouldn't the default expiration time be zero so the cookie expires when the session is ended?

It's not ideal or obvious, but for a work around, we can add an explicit zero for the expiration time like this:

PHP Code:
$this->input->set_cookie('XDEBUG_PROFILE'$set0); 

Should I submit a bug report for this or am I missing something?
https://github.com/bcit-ci/CodeIgniter/issues/4904

(01-25-2018, 11:36 AM)pbarney Wrote: [ -> ]It's not ideal or obvious, but for a work around, we can add an explicit zero for the expiration time like this:

PHP Code:
$this->input->set_cookie('XDEBUG_PROFILE'$set0); 

Actually, that is ideal, at least as far as what you meant goes ... Because it's explicit, and I've learned the hard way - being explicit is always better.

The reason why this bug could ever happen is that we have default parameter values at all. If I was designing this today, you'd be always required to explicitly specify the first 3 parameters, but this goes waaaay back in history, before I ever got involved.

What's not ideal is that you don't need to use CI_Input or anything within CodeIgniter to set a cookie. Honestly, there's zero benefit to that; just use setcookie().