Jump to content

cookie stopped working


Guest

Recommended Posts

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:
 

Quote

$articleid = "test";
$domain = "[redacted]";

$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:
 

 

 
Quote

 

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/278377-cookie-stopped-working/
Share on other sites

Ok let's start with the first part

 

https://[redacted]/cookie1.php

 

Quote

$articleid = "test";
$domain = "[redacted]";

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

 

Quote

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/[redacted]/public_html/cookie1.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/[redacted]/public_html/cookie1.php:11) in /home/[redacted]/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.

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.

  • 2 weeks later...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.