Title: php cookie stores wrong post ID
Last modified: August 19, 2016

---

# php cookie stores wrong post ID

 *  Resolved [mindspins](https://wordpress.org/support/users/mindspins/)
 * (@mindspins)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/php-cookie-stores-wrong-post-id/)
 * 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](https://wordpress.org/support/users/mindspins/)
 * (@mindspins)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/php-cookie-stores-wrong-post-id/#post-1696174)
 * 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 =(
 *  [thepcruinedmylife](https://wordpress.org/support/users/thepcruinedmylife/)
 * (@thepcruinedmylife)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/php-cookie-stores-wrong-post-id/#post-1696514)
 * 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 ](http://wordpress.org/support/topic/how-can-i-remove-these-extra-lines-added-inside-ltheadgt-tags-after-28-upgrade)
   the cookie set correctly in Firefox.
 * Hope this helps you.
 *  Thread Starter [mindspins](https://wordpress.org/support/users/mindspins/)
 * (@mindspins)
 * [15 years ago](https://wordpress.org/support/topic/php-cookie-stores-wrong-post-id/#post-1696515)
 * **Thanks a lot!** Had found a same solution [here](http://www.ebrueggeman.com/blog/wordpress-relnext-and-firefox-prefetching/)

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘php cookie stores wrong post ID’ is closed to new replies.

## Tags

 * [cookie](https://wordpress.org/support/topic-tag/cookie/)
 * [Firefox](https://wordpress.org/support/topic-tag/firefox/)
 * [Post_ID](https://wordpress.org/support/topic-tag/post_id/)

 * 3 replies
 * 2 participants
 * Last reply from: [mindspins](https://wordpress.org/support/users/mindspins/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/php-cookie-stores-wrong-post-id/#post-1696515)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
