WordPress.org

Ready to get started?Download WordPress

Forums

Having trouble setting cookie in wordress: hit counter (2 posts)

  1. DazVision
    Member
    Posted 3 years ago #

    Hi, hope someone can help.
    I have a small amount of php code in my theme to output a hit counter. My client wants this feature. The counter only increases on unique users and this is maintained by saving a cookie.
    The php code works fine if is saved to a separate php file (modified) and accessed directly but it just wont allow me to set a cookie from within wordpress.

    My existing code:

    $file = (TEMPLATEPATH."/count.txt");
    $count_unique = 1;
    $cn = 'gcc';
    $count = intval(trim(file_get_contents($file)));
    if ($count_unique == 0 || !isset($_COOKIE[$cn])) {
    ++$count;
    file_put_contents($file, $count);
    setcookie($cn, '1', time()+3600, '/','.localhost');
    }
    echo $count;

    The domain in setcookie is localhost because I'm building locally.

    Any help on this would be greatly appreciated, whether it be on the code or on a hit counter plugin (that works for unique visits).

    Thank you in advance.

  2. DazVision
    Member
    Posted 3 years ago #

    I found some code relating to WP-constants but still nothing :(
    setcookie($cn, 1, time()+3600, COOKIEPATH, COOKIE_DOMAIN);

    Please help if you know the answer.

Topic Closed

This topic has been closed to new replies.

About this Topic