Jump to content

cookie stopped working


Go to solution Solved by moylin,

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:
 

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

 

Link to comment
Share on other sites

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 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
Link to comment
Share on other sites

  • Solution

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
Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.