www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: [RESOLVED] Cookie problem

  1. #1
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101

    resolved [RESOLVED] Cookie problem

    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>

    <script type="text/javascript" language="javascript">
    var currentStyle = getDataCookie('styleName');
    document.write ('<link rel="stylesheet" title="default" type="text/css" href="http://www.millesaisons.fr/millesaisons/wp-content/themes/default-fr/' + currentStyle + '.css" />');

    </script>
    So, obviously, this is the php calling the cookie. But the cookie creation script is here :

    HTML Code:
    // JavaScript Document
    function getWholeCookie(offset) {
    	var endstr = document.cookie.indexOf (';', offset)
    	if (endstr ==-1) {
    		endstr = document.cookie.length;
    	}
    	return unescape(document.cookie.substring(offset,endstr));
    }
    
    function getCookie(name) {
    	var arg = name + '=';
    	var argLength = arg.length;
    	var cookieLength = document.cookie.length;
    	var i = 0;
    	while (i < cookieLength) {
    		var j = i + argLength;
    		if (document.cookie.substring(i,j) == arg) {
    			return getWholeCookie(j);
    		}
    		i = document.cookie.indexOf(' ',i) + 1;
    		if (i==0) break;
    	}
    	return '';
    }
    
    
    function setCookie(name,value,expires,path) {
    var today = new Date();
    today.setTime( today.getTime() );
    
    if ( expires )
    {
    expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
    ( ( path ) ? ";path=" + path : "" );
    }
    
    
    function tossCookie( name, path, domain ) {
    if ( getCookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
    
    
    function getDataCookie(cookiename) {
    	var data = '';
    	if (navigator.cookieEnabled) data = getCookie(cookiename);
    	if (data == '') data = 'fantasy';
    	return data;
    }
    function getDataCookiedim(cookiename) {
    	var data = '';
    	if (navigator.cookieEnabled) data = getCookie(cookiename);
    	if (data == '') data = 'style';
    	return data;
    }
    function changeStyle(newStyle) {
    	if (navigator.cookieEnabled) {
    	setCookie('styleName',newStyle,31,'/');
    	location.href = self.location;
    	}
    	else alert ('Vous avez besoin d\'activer javascript pour profiter pleinement du site');
    }
    function changeStyledim(newStyle) {
    	if (navigator.cookieEnabled) {
    	setCookie('styleNamedim',newStyle,31,'/');
    	location.href = self.location;
    	}
    	else alert ('Vous avez besoin d\'activer javascript pour profiter pleinement du site');
    }
    
    function lien(nom,src) {	
    	document.images[nom].src=src;}
    I wrote this script more than 2 years ago, and as I'm not a javascript person, I browsed the internet and put pieces together to make this

    Now, my problem is that styleName is created at blog.domain.com and I want it at domain.com. How do I do that ?
    Thanks a lot !!!

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Try setting
    Code:
    path : "/"
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    and
    Code:
    domain : ".mysite.com"
    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

  5. #5
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    Still not working I'm really pissed off, I was so proud of this website, and now it's full of errors, 404 pages, etc.

  6. #6
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Security restrictions
    There is also localStorage and sessionStorage.
    According to MS the localStorage may work for you.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  7. #7
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    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 :/

  8. #8
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Just alert(document.cookie) in the sub-domain
    At least 98% of internet users' DNA is identical to that of chimpanzees

  9. #9
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    ??? Where in my code, and doesn't alert (if I remember from the year I tried to learn javascript 10 years ago) displays a popup ?

  10. #10
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Anywhere, just to check if any cookies have been set.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  11. #11
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    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

  12. #12
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    The cookie is created in the root.
    The cookie is not accessible from page.php, document.cookie returns empty from that page.
    Is this what occurs?
    At least 98% of internet users' DNA is identical to that of chimpanzees

  13. #13
    Join Date
    Aug 2004
    Location
    Switzerland
    Posts
    1,101
    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.

  14. #14
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    If it's created in the sub-domain it is only accessible from there.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    If you are already using PHP to check the cookie:
    if (!empty($_COOKIE['styleName'])) { ?>
    </noscript>

    <script type="text/javascript" language="javascript">
    var currentStyle = getDataCookie('styleName');
    document.write ('<link rel="stylesheet" title="default" type="text/css" href="http://www.millesaisons.fr/millesaisons/wp-content/themes/default-fr/' + currentStyle + '.css" />');

    </script>
    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) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
    }

    Found here: http://techpatterns.com/downloads/ja...pt_cookies.php

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles