Hi! (I posted this on the PHP forum, but apparently it's more a javascript problem, so I'm posting this here too)
I made a website for a client 2 years ago, and the guys had the great idea of putting the website on a subdomain. I had a cool cookie system, that allowed the user to choose between four different styles. Now I have a problem with my cookie system. The cookie is created on :
blog.domain.com
and apparently it's read on domain.com, so it's not found. I searched everywhere, but I don't know how to specify that it has to look inside blog.domain.com, or maybe it would be easier to directly create the cookie at domain.com instead of blog.domain.com ?
That's the cookie called styleName that I'm talking about. It was situated at domain.com and it worked flawlessly, and now its at blog.domain.com and it's considered as empty by my code...
Here is my code :
PHP Code:
<?php
if (!empty($_COOKIE['styleName'])) { ?>
</noscript>
It should be possible to set the cookie in domain and read it in sub-domain, but not the other way around. These security restrictions may may between browsers.
At least 98% of internet users' DNA is identical to that of chimpanzees
It's really not possible to read the cookie at blog.domain.com ? It seems weird, since if I use a subdomain and decide to work with cookies, it must surely work. I don't care if the cookie is created at domain.com or at blog.domain.com, but the reading of the cookie must be from the same domain the cookie is created...
I looked into the $_COOKIE description, but I couldn't find a way to tell it that the cookie was on the subdomain :/
I know the cookie has been set, I can find it in my cookies : styleName at blog.domain.com
I tried to remove it and choose my style again, the cookie is indeed created, but can't be read...
The problem is that the website is still at the root of the domain. I mean, the direct link to the page that needs to load the cookie is something like http://www.domain.com/blabl/blabl/page.php
No, not exactly: the cookie is created in blog.domain.com and isn't accessible from page.php, whose direct link is something like domain.com/blogname/wp_content/page.php (it's a wordpress website).The weird thing is why is the cookie not created in the root, but on the subdomain...
Before they messed up the website, the cookie was created in the root.
Why not use PHP to get/set the cookie? You can set the domain on a PHP initialized cookie very easily.
Also a 2 second google search found a way to set the domain on a javacscript cookie:
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );
/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );
Bookmarks