Forums

[resolved] php cookie stores wrong post ID (4 posts)

  1. mindspins
    Member
    Posted 1 year ago #

    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?!

  2. mindspins
    Member
    Posted 1 year ago #

    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 =(

  3. thepcruinedmylife
    Member
    Posted 1 year ago #

    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.

  4. mindspins
    Member
    Posted 1 year ago #

    Thanks a lot! Had found a same solution here

Topic Closed

This topic has been closed to new replies.

About this Topic