gdfhghjdfghgfhf Posted May 25, 2013 Share Posted May 25, 2013 I've been using this script since a few months and it was working perfectly until i switched host (and php version)Basically the script is used to put the page ID inside an array in a cookie, each time a page is visited.Then on another page, the list of the last visited page ID'sHere is the first part that will put the page ID inside a cookie: $articleid = "test";$domain = "ni-dieu-ni-maitre.com";$lastviewedarticles = array();if (isset($_COOKIE["viewed_articles"]) ) {$lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);}if (!in_array($articleid, $lastviewedarticles)){$count = count($lastviewedarticles);if($count>=50)array_shift($lastviewedarticles);$lastviewedarticles[] = $articleid;}$cookiedomain = ".$domain";setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', $cookiedomain); And now the second part that will output the list of the last 5 visited page id's: if ( isset($_COOKIE["viewed_articles"]) ) {$lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);}$lastviewedarticles = array_reverse($lastviewedarticles);$numrecent = count($lastviewedarticles);if ($numrecent >= 1) {$i = 0;foreach ($lastviewedarticles as $recent) {echo "$recent<br>";}$i++;if($i >= 4) {break;}}} Any idea why it suddently stopped working ? I don't remember having edited something since a while. Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/ Share on other sites More sharing options...
requinix Posted May 25, 2013 Share Posted May 25, 2013 "Stopped working" how? Errors or warnings? Maybe ones that your setup has hidden from you? What does it do and how does that compare to what it's supposed to do? Details. You know these things, not us. Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1432207 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted May 27, 2013 Author Share Posted May 27, 2013 (edited) Ok let's start with the first part https://www.ni-dieu-ni-maitre.com/cookie1.php $articleid = "test";$domain = "ni-dieu-ni-maitre.com";$lastviewedarticles = array();if (isset($_COOKIE["viewed_articles"]) ) {$lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);}if (!in_array($articleid, $lastviewedarticles)){$count = count($lastviewedarticles);if($count>=50)array_shift($lastviewedarticles);$lastviewedarticles[] = $articleid;}$cookiedomain = ".$domain";setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', $cookiedomain); First time you load the page (before cookie is created) it works, but as soon as the cookie already exist the code try to add the $articleid to the existing array but the page returns an error. Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/anarchoi/public_html/cookie1.php on line 11Warning: Cannot modify header information - headers already sent by (output started at /home/anarchoi/public_html/cookie1.php:11) in /home/anarchoi/public_html/cookie1.php on line 18 I already explained what it's supposed to do but let me be more precise: This code is used in a t-shirts shop. Each t-shirts has it's own page and each t-shirt has an article ID. On each t-shirt's page, the code i posted above is running and it is supposed to add the current articleid to an array (lastviewedarticles) On another page, another script (the code i posted in the second quote of my first post) will extract the articles ID from the array inside the cookie and display a list of the last visited articles ID The objective is to list the last 5 t-shirts viewed by the users, using the articles ID. Edited May 27, 2013 by ungovernable Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1432465 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted May 28, 2013 Author Share Posted May 28, 2013 bump Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1432693 Share on other sites More sharing options...
Solution moylin Posted May 28, 2013 Solution Share Posted May 28, 2013 (edited) Check if Magic quotes GPC is on, if you switched to a shared host, a lot of them seem to like to leave it on. in the php.ini add magic_quotes_gpc =0 you can confirm if this is on by checking phpinfo(). if you have trouble with that, you can always stripslashes. edit: added extra note. Edited May 28, 2013 by moylin Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1432708 Share on other sites More sharing options...
mac_gyver Posted May 28, 2013 Share Posted May 28, 2013 you need to troubleshoot and find out what exactly is in $_COOKIE["viewed_articles"]. it's either empty or it's escaped, due to what moylin posted above, and it produced a notice error when you tried to unseralize it. Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1432709 Share on other sites More sharing options...
gdfhghjdfghgfhf Posted June 7, 2013 Author Share Posted June 7, 2013 Check if Magic quotes GPC is on, if you switched to a shared host, a lot of them seem to like to leave it on. in the php.ini add magic_quotes_gpc =0 you can confirm if this is on by checking phpinfo(). if you have trouble with that, you can always stripslashes. edit: added extra note. i switched to a dedicated server and yes, magic quotes are on. i just turned it off and it is working again thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/#findComment-1434735 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.