SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Set default cookie

    HI all,

    I'm working with a stylesheet switcher which sets the value of the new theme name inside a cookie. Then I'm pulling that value in through the link's href.

    Is there an easy way to set a default value without having it overwrite the new cookie value on refresh? Currently my default value continually overwrites the chosen theme on page load.

    In my header:
    Code:
    <?php
    setcookie("newtheme", "theme_a", time()+3600);
    ?>
    
    <link rel="stylesheet" id="new-theme" type="text/css" href="/wp-content/themes/<? echo $_COOKIE["newtheme"]; ?>/css/style.css" />

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (empty($_COOKIE['newtheme'])) {
        
    // set a default


  3. #3
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply!

    So i've set:

    Code:
    <?PHP 
    	if (empty($_COOKIE['newtheme'])) { 
    		setcookie("newtheme", "theme_a", time()+3600);
    	} 
    ?>
    When I select a new theme, "theme_b", the cookie sets for 10 days. Then I refresh the page and it returns to "theme_a" for some reason.

    I'm using the jQuery cookie plugin to set the theme cookie on click.

    Code:
    $.cookie('newtheme', themename,  { path: '/', expires: 10 });

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check your cookies to make sure your javascript code is working as expected.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've set an alert after setting the new theme to display the cookie value. It seems to be setting correctly

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    opps - sorry it's working properly. I just had to clear my cookies.
    Although, it does take 2 refreshes to set the default cookie. Any ideas why that why be?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php's $_COOKIE array contains the values that were sent to it by the browser. setcookie() does not manually update the array, it just tells the browser to send a cookie the next time it makes an http request. so, $_COOKIE won't be updated until the next http request.

    You can use a seperate variable which you assign either the default value, or the value from $_COOKIE, depending on whether or not $_COOKIE has the value you want.

  8. #8
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh i see
    thanks a bunch!


Bookmarks

Posting Permissions

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