WordPress.org

Ready to get started?Download WordPress

Forums

More on Setting Cookie in Head (8 posts)

  1. dave_merwin
    Member
    Posted 8 years ago #

    So coldforged help me get the basics working. Thanks so much! However, while this seems to be working ok most of the time, when I try to look at a post, the cookie I am writing to is reset to default. I have posted the code below. Any thoughts would be great.

    A little note, inorder to load different themes, and different colors for each theme, I use the folowing to set the theme dir, the color dir of that theme, and the font size.

    Let me know. Thanks.

    //This Controls Theme Choice
    if (isset($_GET['theme'])){
    setcookie ('styletheme', $_GET['theme'], time()+31536000, '/', 'localhost', '0');
    $theme = $_GET['theme'];
    } elseif (isset($_COOKIE['styletheme'])) {
    $theme=$_COOKIE['styletheme'];
    } else {
    setcookie ('styletheme', 'beach', time()+31536000, '/', 'localhost', '0');
    $theme = 'beach';
    }

    //This Controls Color
    if (isset($_GET['color'])){
    setcookie ('stylecolor', $_GET['color'], time()+31536000, '/', 'localhost', '0');
    $color = $_GET['color'];
    } elseif (isset($_COOKIE['stylecolor'])) {
    $color=$_COOKIE['stylecolor'];
    } else {
    setcookie ('stylecolor', 'color-1', time()+31536000, '/', 'localhost', '0');
    $color = 'color-1';
    }

    //This Controls Font Size
    if (isset($_GET['set'])){
    setcookie ('sitestyle', $_GET['set'], time()+31536000, '/', 'localhost', '0');
    $sitestyle = $_GET['set'];
    }
    else if (isset($_COOKIE['sitestyle'])) {
    $sitestyle=$_COOKIE['sitestyle'];
    } else {
    setcookie ('sitestyle', 'small', time()+31536000, '/', 'localhost', '0');
    $sitestyle = 'small';
    }

  2. ColdForged
    Member
    Posted 8 years ago #

    It's because you're setting an explicit path. Therefore, every subdirectory on your site will get it's own cookie. Bleh. Remove the parameters to setcookie() after the expiration time.

  3. dave_merwin
    Member
    Posted 8 years ago #

    Wow. Thank you so much. My favorite part is the "bleh". LOL. You were right. Thanks.

  4. ColdForged
    Member
    Posted 8 years ago #

    :)

  5. ideadude
    Member
    Posted 7 years ago #

    ColdForged, I have a similar problem. Simplifying the setcookie code didn't work for me.

    According to the setcookie manual: "If [the path is] set to '/', the cookie will be available within the entire domain." http://us3.php.net/setcookie

    So something like this should work, no?

    setcookie("name", "value", time()+3600*24*30, "/", ".domain.net", 0);

    But my code, when included from a wp page, doesn't get the cookie value. (It does detect the cookie through the isset function though.)

    I tried these two changes, which didn't seem to work either:

    setcookie("name", "value, time()+3600*24*30);

    and

    setcookie("name", "value", time()+3600*24*30, "/", ".domain.net", 0);
    setcookie("name", "value", time()+3600*24*30, "/blog", ".domain.net", 0);

    Any ideas?

  6. ideadude
    Member
    Posted 7 years ago #

    My guess is that Dave's problem was more with the "localhost" part of his call than the "/" part.

    EDIT: On second thought, that doesn't make sense.

  7. ideadude
    Member
    Posted 7 years ago #

    Ok. I've got it working for me. It seems like the problem was on the retreaval of the cookie, not the setting of it. I changed this line:

    $c_password = $HTTP_COOKIE_VARS["Winelog_Password"];

    to

    $c_password = $_COOKIE["Winelog_Password"];

  8. moshu
    Member
    Posted 7 years ago #

    ideadude...
    it is beneficial to look at the DATE of posts/topics you are posting to.
    In April 2005 (!) there was a totally different WP version.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags