• Resolved dave_merwin

    (@dave_merwin)


    I am trying to make a php based style switcher. The switcher code works great. Howerer, I can not set the cookie.

    I have placed the following in my header.php file.
    `<?php
    setcookie (‘sitestyle’, $set, time()+31536000, ‘/’, ‘localhost’, ‘0’);
    ?>’

    It will not set the cookie and I have no idea why. It is above the doc type decleration as required but it does not seem to want to work. Any tips would be great.

    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • What is $set set to?

    Thread Starter dave_merwin

    (@dave_merwin)

    They are set by clicking a href. Sample here:


    <ul>
    <li><a href="?set=beach-green-small" class="small-font">A</a></li>
    <li><a href="?set=beach-green-medium" class="medium-font">A</a></li>
    <li><a href="?set=beach-green-large" class="large-font">A</a></li>
    </ul>

    Ahhh, is it not being set because the first itteration does not have a variable? Wow, needs $_GET doesn’t it?

    And a case for when it isn’t set. Because if it ain’t set then calling setcookie() with an empty value parameter deletes the cookie :).

    Thread Starter dave_merwin

    (@dave_merwin)

    ahhhh ok. How about this?


    if (isset($_GET['set'])){
    setcookie ('sitestyle', $_GET['set'], time()+31536000, '/', 'localhost', '0');
    }

    if (isset($_COOKIE['sitestyle'])) {
    $sitestyle=$_COOKIE['sitestyle']
    } else {
    setcookie ('sitestyle', 'beach-green-small', time()+31536000, '/', 'localhost', '0');
    }

    Almost. The only problem with the logic is testing if the cookie is set. During the entire first invocation of that page when you do your setcookie() when $_GET['set'] is set the cookie doesn’t exist in the $_COOKIE array. Only when the page next loads is the cookie available there. So, something like this:

    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', 'beach-green-small', time()+31536000, '/', 'localhost', '0');
    $sitestyle = 'beach-green-small';
    }

    May not be optimal, but that’s the kind of logic needed.

    Thread Starter dave_merwin

    (@dave_merwin)

    OH, baby we are in business! Thanks for your help.

    No problem, glad you got it working.

    Thanks! Had the same problem, added:

    $sitestyle=$_COOKIE[‘sitestyle’];

    …now it works!

    pshames

    (@pshames)

    I also use $_COOKIE[‘sitestyle’] in my header, which works great on my home page and the index.php (with a list of recent blog entries). However, the variable is NULL when I go to one of my blog entries (or archive, catgeories, etc.). The index.php is in the root and the other files (e.g. single.php) are in a theme sub-directory, but what is the problem?

    I’m trying to create a plugin which needs to deal with cookies. I’d like to include the cookie “logic” inside a function in my plugin insetad of putting all the code in the header.php.

    E.g. I created a plugin called cookie_logic.php containing this:

    cookie_logic() {

    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’, ‘beach-green-small’, time()+31536000, ‘/’, ‘localhost’, ‘0’);
    $sitestyle = ‘beach-green-small’;
    }

    } //end cookie_logic()

    Then in the header.php I put:

    <?php cookie_logic(); ?>

    But it doesn’t work. Why? Can’t a cookie be set if called by a plugin?

    Thanks for helping.

    I replied to my question myself. So if you want to set a cookie from your own plugin (as in my case) you can do it as follows:

    create your plugin.php, put the plugin name, authoer etc. and insert the function as follows:

    cookiestart() {

    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’, ‘beach-green-small’, time()+31536000, ‘/’, ‘localhost’, ‘0’);
    $sitestyle = ‘beach-green-small’;
    }

    return $sitestyle;

    } //end cookie_logic()

    Then in the header.php of our theme put the following:

    <?php
    $sitestyle = cookie_logic();
    ?>

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Setting a cookie in header’ is closed to new replies.