Welcome Guest, Not a member yet? Register   Sign In
$this->input->cookie and $config['cookie_prefix']
#1

Hello CI Community!
I've been using cookies and noticed that having $config['cookie_prefix'] set and using $this->input->cookie works weird.
Assuming $config['cookie_prefix'] = 'myprefix_'
Then setting cookie in code with
Code:
$cookie = array(
   'name' => 'somecookie',
   'value' => '1',
   'expire' => 3600
);
$this->input->set_cookie($cookie);

And then trying to fetch it with
Code:
$this->input->cookie('somecookie')
Results NULL being returned.
Code:
$this->input->cookie('myprefix_somecookie')
This example on the other hand, returns the correct value.
Shouldn't this call respect cookie_prefix setting?
Reply
#2

If it did, how would you read non-prefixed cookies?
Reply
#3

(04-09-2016, 05:25 PM)Narf Wrote: If it did, how would you read non-prefixed cookies?

Well, if we're to maintain BC, then there should be a new method added like 'input->prefixed_cookie' with original cookie() signature to read prefixed cookies, but IMHO, that should have been an original cookie() behavior with ucookie method or additional param in cookie() to read unprefixed cookies, since current set_cookie autoprefix cookie names based on cookie_prefix setting.
Reply
#4

Went through the code, found get_cookie which checks if there's a cookie without prefix and if it is, uses it, otherwise, it use prefixed cookie name. However that would not work in my case.
I have top level site name for prod: sitename.info
Then I have dev.sitename.info and qa.sitename.info. Top level site conflicts with other sites, that's why I'm utilizing the cookie_prefix to namespace those environments from the top level one.
Reply
#5

(This post was last modified: 04-10-2016, 09:25 AM by PaulD. Edit Reason: Added PS )

Hi,

I do not agree. I often want to read cookies that are not CI cookies. All you have to do is name your cookies correctly. If you are reading a cookie set with a prefix, get the prefix name and use it.

PHP Code:
$this->config->item('cookie_prefix'); 

http://www.codeigniter.com/user_guide/li...nfig-items

If CI added config prefix to every cookie request, people would be complaining about that.

Best wishes,

Paul.

PS You can edit the config setting too on the fly: http://www.codeigniter.com/user_guide/li...onfig-item
You can change it to match which part of the site you are on if you want.
In your circumstance, I would remove the cookie prefix entirely and set and read cookies named depending on which bit of the site your are using.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB