Jump to content


Photo

cookie stopped working


Best Answer moylin, 28 May 2013 - 06:07 AM

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.

Go to the full post


  • Please log in to reply
6 replies to this topic

#1 ungovernable

ungovernable

    Advanced Member

  • Members
  • PipPipPip
  • 212 posts

Posted 25 May 2013 - 01:15 AM

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's

Here 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.

 



#2 requinix

requinix

    Playful Member

  • Moderators
  • 5,744 posts
  • LocationWA

Posted 25 May 2013 - 04:01 AM

"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.

#3 ungovernable

ungovernable

    Advanced Member

  • Members
  • PipPipPip
  • 212 posts

Posted 27 May 2013 - 12:45 AM

Ok let's start with the first part

 

https://www.ni-dieu-...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 11

Warning: 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 by ungovernable, 27 May 2013 - 12:48 AM.


#4 ungovernable

ungovernable

    Advanced Member

  • Members
  • PipPipPip
  • 212 posts

Posted 28 May 2013 - 04:36 AM

bump



#5 moylin

moylin

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 28 May 2013 - 06:07 AM   Best Answer

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 by moylin, 28 May 2013 - 06:10 AM.


#6 mac_gyver

mac_gyver

    Advanced Member

  • Moderators
  • 1,890 posts

Posted 28 May 2013 - 06:11 AM

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.


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#7 ungovernable

ungovernable

    Advanced Member

  • Members
  • PipPipPip
  • 212 posts

Posted 07 June 2013 - 02:52 PM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com