-1

this my code but i am not getting the my cookie value in page

if (isset($_COOKIE['mycookie'])) {

    $cookie[] = $proID;
    $cookie = implode(',', $cookie);
    $cookie = unserialize($cookie);
    setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/');

}
Bender
  • 705
  • 11
  • 25
Gomez
  • 11
  • 2
  • Where do you set $proID ? – Spikey21 Aug 03 '15 at 08:32
  • i am posting to my page – Gomez Aug 03 '15 at 08:33
  • if the cookie is not set i use $cookie=array(); $cookie[] = $proID; $cookie = serialize($cookie); setcookie('mycookie',$cookie,time() + (86400 * 30),"/"); – Gomez Aug 03 '15 at 08:33
  • why would you first `implode` an array than `unserialize` to `serialize` it in the next step. Looks to me as unnecessary – SuperDJ Aug 03 '15 at 08:38
  • Possible duplicate: http://stackoverflow.com/questions/3421154/how-to-add-edit-a-cookie-in-php – IgnazioC Aug 03 '15 at 08:40
  • Please help me to how we set a cookie and add extra value to that cookie on each time when we on the page – Gomez Aug 03 '15 at 08:40
  • $cookie_data['foo'] = 'bar'; setcookie("mycookies",serialize($cookies_array),time()+60*60*24*30); didnt understand $cookie_data['foo'] = 'bar'; – Gomez Aug 03 '15 at 08:43

2 Answers2

1

You are doing isset($_COOKIE['mycookie']) which will check if the $_COOKIE['mycookie'] is set or not, will return true if set else false. And if you are setting it inside the if block for the first time it will never be set. So it should be -

if(!isset($_COOKIE['mycookie']))
    // Set the cookie   
}
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • $cookie=array(); $cookie[] = $proID; $cookie = serialize($cookie); setcookie('mycookie',$cookie,time() + (86400 * 30),"/"); if cookie not set – Gomez Aug 03 '15 at 08:34
  • Then you just have to add a `negation` to `isset`. Check the answer. – Sougata Bose Aug 03 '15 at 08:35
  • sorry i didnt understand what u said – Gomez Aug 03 '15 at 08:36
  • if (isset($_COOKIE['mycookie'])) { $cookie[] = $proID; $cookie = implode(',', $cookie); $cookie = serialize($cookie); setcookie('mycookie', $cookie, time() + (86400 * 30), '/'); } else{ $cookie=array(); $cookie[] = $proID; $cookie = serialize($cookie); setcookie('mycookie',$cookie,time() + (86400 * 30),"/"); } this my whole code – Gomez Aug 03 '15 at 08:46
0

First get the old value of the cookie, and turn it into an array with unserialize. If the cookie isn't set, initialize it to an empty array.

if (isset($_COOKIE['mycookie'])) {
    $cookie = unserialize($_COOKIE['mycookie']);
} else {
    $cookie = array();
}

Then add the new element to the array, and save it back into the cookie.

$cookie[] = $proID;
setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/');
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks , i tried but value is not appending if the cookie already set the code is used : if (isset($_COOKIE['mycookie'])) { $cookie = unserialize($_COOKIE['mycookie']); $cookie[] = $proID; setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/'); } else { $cookie = array(); $cookie[] = $proID; setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/'); } – Gomez Aug 03 '15 at 08:57
  • Do you get an error? Are you reloading the page when you do this? `$_COOKIE` doesn't get updated when you call `setcookie()`, only when the script first starts. – Barmar Aug 03 '15 at 09:03
  • a:1:{i:0;s:2:\"12\";}
    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent each time when i go to different product it update but i cant see the previous product id
    – Gomez Aug 03 '15 at 09:12
  • http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Barmar Aug 03 '15 at 09:15
  • but i cant see the data appending each updates i got different id not showing two or more id – Gomez Aug 03 '15 at 09:16
  • If you get the `headers already sent` error, it doesn't update the cookie. You have to fix that, and the question I linked to explains how. – Barmar Aug 03 '15 at 09:17
  • warning is shwing beacuse as a testing i am echoing the cookie value – Gomez Aug 03 '15 at 09:17
  • Don't do that. You can use the browser's console to see cookies. – Barmar Aug 03 '15 at 09:18
  • $cookie[] = $proID; setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/'); $cookie[] is not appending data to the old value it updating the id with new id – Gomez Aug 03 '15 at 09:19
  • i tried but but still new value not appending to old data – Gomez Aug 03 '15 at 09:24
  • I just tried it and it works for me. Every time I reload the page, another element is added to the cookie. – Barmar Aug 03 '15 at 09:26
  • mycookies a:1:{i:0;s:2:"17";} – Gomez Aug 03 '15 at 09:27
  • another page it showing mycookies a:1:{i:0;s:2:"17";} – Gomez Aug 03 '15 at 09:28
  • Make sure all of them specify the pathname `/` when setting the cookie. By default they use the directory that the script is in. – Barmar Aug 03 '15 at 09:29
  • if (isset($_COOKIE['mycookie'])) { $cookie = array(); $cookie = unserialize($_COOKIE['mycookie']); $cookie[] = $proID; $cookie = implode(',', $cookie); setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/'); } else { $cookie = array(); $cookie[] = $proID; setcookie('mycookie', serialize($cookie), time() + (86400 * 30), '/'); } this is my code can u p-lease check it is working for u – Gomez Aug 03 '15 at 09:31
  • Don't use `implode(',', $cookie)`. – Barmar Aug 03 '15 at 09:34
  • Use the code exactly as it is in my answer. I tested it and it works. – Barmar Aug 03 '15 at 09:34
  • it tried it shows a:1:{i:0;s:2:"17";} on the first page and a:1:{i:0;s:2:"12";} – Gomez Aug 03 '15 at 09:39
  • Sorry, I can't tell what you're doing differently. – Barmar Aug 03 '15 at 09:40
  • i used ur code but when ever i got new product page i am getting the cookie but it nor showing the previous product id on that – Gomez Aug 03 '15 at 09:43
  • I don't know what to tell you, sorry. – Barmar Aug 03 '15 at 09:47