• Resolved mindspins

    (@mindspins)


    Using the following code in the header.php:

    global $wp_query;
    $post_id = $wp_query->post->ID;
    $cookietime = time()+3600*24*365;
    setcookie('tst', $post_id, $cookietime, '/', '.domain.com');

    Every browser returns the right post id except Firefox (3.6.10 on windows). Firefox first writes the right id of the post, but then overwrites it whit the one published after the one it’s showing. I’ve checked it several ways! In other words, ik I write the post id to a variable and store the variable in a cookie I’ll get two id’s in one pageview. Like so:

    global $wp_query;
    $post_id = $wp_query->post->ID;
    $cookietime = time()+3600*24*365;
    $lt = '|'
    if (isset($_COOKIE['tst'])) {
    	$viewed = $_COOKIE['tst'];
    	$viewedstring = substr($viewed, 0, -1);
    	$viewedstring .= $post_id.$lt;
    	setcookie('tst', $viewedstring, $cookietime, '/', '.domain.com');
    }else{
    	$viewedstring = $post_id.$lt;
    	setcookie('tst', $viewedstring, $cookietime, '/', '.domain.com');
    }

    returns 421|427| in one pageview.

    What’s happening?!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mindspins

    (@mindspins)

    Sorry second code should be

    global $wp_query;
    $post_id = $wp_query->post->ID;
    $cookietime = time()+3600*24*365;
    $lt = '|'
    if (isset($_COOKIE['tst'])) {
    	$viewedstring = $_COOKIE['tst'];
    	$viewedstring .= $post_id.$lt;
    	setcookie('tst', $viewedstring, $cookietime, '/', '.domain.com');
    }else{
    	$viewedstring = $post_id.$lt;
    	setcookie('tst', $viewedstring, $cookietime, '/', '.domain.com');
    }

    Problem in Firefox stays =(

    Hi #mindspins

    I too had this problem and after a fair amount of thought I figured it may have something to do with picking up on the “previous” and “next” page link automatically embedded into the header of a WordPress site. When I removed these links using this from #greenshady the cookie set correctly in Firefox.

    Hope this helps you.

    Thread Starter mindspins

    (@mindspins)

    Thanks a lot! Had found a same solution here

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘php cookie stores wrong post ID’ is closed to new replies.