Adding "get_cookie" function to wordpress?? Help!!

Discussion in 'PHP' started by Cyrus255, Apr 30, 2011.

  1. #1
    I have a code for wordpress that will basically say "hello" to first time visitors if they don't have a cookie that is set when they visit the first time. But I get this error:

    <?php

    if (!get_cookie('firstvisit')) {?> hello <?php } ?>
    // cookie not set, first visit

    // create cookie to avoid hitting this case again
    $cookie = array(
    'name' => 'firstvisit',
    'value' => 'Yes',
    'expire' => time()+86500,
    'domain' => 'mysite.com',
    'path' => '/',
    'prefix' => '',
    );
    set_cookie($cookie);
    }
    ?>

    How do I do this in wordpress?
     
    Cyrus255, Apr 30, 2011 IP
  2. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <?php
    
    if (!isset($_COOKIE['firstvisit'])) {
    echo('Hello');
    // cookie not set, first visit
    
    // create cookie Eto avoid hitting this case again
    $cookie = array(
    'name' => 'firstvisit',
    'value' => 'Yes',
    'expire' => time()+86500,
    'domain' => 'mysite.com',
    'path' => '/',
    'prefix' => '',
    );
    setcookie($cookie);
    }
    ?>
    PHP:
     
    TimK, Apr 30, 2011 IP
  3. blakeembrey

    blakeembrey Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A better way to do this without setting any extra cookies is by:

    
    if(!isset($_COOKIE)) {
    // No cookies set. First visit.
    echo "Hello";
    }
    
    PHP:
    This is useful if you already have analytics or something else enabled that sets cookies anyway, since there is no point wasting space on browsers if you don't need to.
     
    blakeembrey, Apr 30, 2011 IP
  4. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #4
    I keep getting this error if I use the code: "setcookie() expects parameter 1 to be string, array given in"
    Even for just that simple Hello echo.
    Please assist.
     
    Cyrus255, May 1, 2011 IP
  5. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <?php
    
    if (!isset($_COOKIE['firstvisit'])) {
    echo('Hello');
    // cookie not set, first visit
    
    // create cookie Eto avoid hitting this case again
    setcookie('firstvisit','Yes', time()+86500, '/', 'mysite.com');
    }
    ?>
    
    PHP:
     
    TimK, May 1, 2011 IP
  6. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #6
    That jumps one hurdle, but I get a "Warning: Cannot modify header information - headers already sent by" at first it said All in One Plugin, but after I disabled the plugin it stays on but said that the line with "setcookie" was what already sent the header info. Makes no sense to me. Any help? Thanks again!
     
    Cyrus255, May 1, 2011 IP
  7. TimK

    TimK Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Place the PHP code at the top of your file.
     
    TimK, May 1, 2011 IP
  8. Cyrus255

    Cyrus255 Well-Known Member

    Messages:
    796
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    150
    #8
    Wow it actually works!! Thanks a million, you're a lifesaver!
     
    Cyrus255, May 1, 2011 IP