• 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.

Viewing 1 replies (of 1 total)
  • Thread Starter DazVision

    (@dazvision)

    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.

Viewing 1 replies (of 1 total)
  • The topic ‘Having trouble setting cookie in wordress: hit counter’ is closed to new replies.