CodeIgniter Forums

Full Version: Cookie Domain Name Problems - That Darn Dot
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]SevenZark[/eluser]
I'm using CodeIgniter's set_cookie() function to set cookies in the browser. I'm doing this, as opposed to using CI's built-in session management, because I have some existing JavaScript code that needs to access the same cookies. The problem is that I need to temporarily "share" those cookies with some direct Ajax calls that the JavaScript makes to an API existing on another system. I'm doing locally-based development, and access my local server as something like 'site.local'

So here's my problem. When a cookie comes down from those Ajax calls, it's associated with the domain "site.local," but when I use CI's set_cookie() function the domain is set as '.site.local' (note the initial dot). In my CI config.php file I have 'cookie_domain' set to 'site.local,' but it seems that CI still automatically forces the initial dot into the cookie's domain parameter. This is causing the Ajax cookies to get out of sync with the ones CI is accessing, because they get set as separate cookies, rather than update the one shared cookie by name.

I realize this isn't an ideal situation, but it's part of a slow transition from the direct Ajax JavaScript handling data, to having CodeIgniter call it and use it in the control layer. I also do not have access to change the way the Ajax API writes the cookies.

Does anybody know how to get rid of this initial dot that CodeIgniter is forcing into the cookies?

El Forum

[eluser]SevenZark[/eluser]
Okay, I think I've solved my own problem. Looks like PHP's built-in setcookie() function also forces the initial dot. This is technically required by the spec for cookies, but what's annoying is that if they're set by an API called by JavaScript, browsers tend to default the domain to the current one for the page, but WITHOUT the initial dot, making it very tough to write code that can 'share' the cookies with JavaScript consuming another API.

The only way I can see to get around this is to write my own function that writes cookies directly in the header itself, which is what it looks like I'm going to have to do.

El Forum

[eluser]rip_pit[/eluser]
hi

My solution is not directly related to CI, because i fixed the problem under a non CI website (shame on me ^^) i'm working on
The solution that worked for me was to set domain = false (I don't have need of subdomains)

Code:
setcookie('name', 'value', (time() + 3600), '/', false);