-1

Not sure what i am doing wrong...

$articleid = "test";

$lastviewedarticles = array();

if (isset($_COOKIE["viewed_articles"]) ) {
  $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
}

if (!in_array($articleid, $lastviewedarticles)){
    $count = count($lastviewedarticles);
    if($count>=29)
        array_shift($lastviewedarticles);
    $lastviewedarticles[] = $articleid;
}
setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', '.' . $domain);

Then this page reads the cookie and output the content:

if ( isset($_COOKIE["viewed_articles"]) ) {
  $lastviewedarticles = unserialize($_COOKIE["viewed_articles"]);
}
echo "cookie is currently:<br>";
print_r($lastviewedarticles);

As you can see on the test pages, the cookies are always empty

456543646346
  • 973
  • 4
  • 15
  • 22

2 Answers2

1
... '.$domain');
    ^        ^

You use single quotes that means that php is not going to replace $domain with respective value from variable:

Docs:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

And since you don't access to cookies from .$domain cookies are not set:

Docs:

domain

The domain that the cookie is available to. Setting the domain to 'www.example.com' will make the cookie available in the www subdomain and higher subdomains. Cookies available to a lower domain, such as 'example.com' will be available to higher subdomains, such as 'www.example.com'. Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

Change it to:

... '.'.$domain);
Leri
  • 12,367
  • 7
  • 43
  • 60
  • Just updated the code... Still doesn't work :( setcookie('viewed_articles', serialize($lastviewedarticles), time()+60*60*24*30, '/', '.'.$domain); – 456543646346 Mar 08 '13 at 09:40
  • @anarchOi Can you post what does `print_r` display? Can you use `var_dump` instead of `print_r`? – Leri Mar 08 '13 at 09:51
  • @anarchOi This means that cookies are set properly but value is not what you expect, check what `serialize($lastviewedarticles)` returns. – Leri Mar 08 '13 at 10:16
  • Check the code i posted, it is supposed to work. i printed $lastviewedarticles just before putting it in the cookie and it is not empty. Also if i use setcookie() without setting the domain name it will work. So it has something to do with setcookie() not being used propely. This will work = setcookie('viewed_articles', serialize($lastviewedarticles)); – 456543646346 Mar 08 '13 at 10:36
  • @anarchOi Take a look at your first sample. It always sets empty array in cookies (if there isn't a cookie, it is empty array, so `if` statement never executes, so in cookies empty array is set. If there's a cookie (which is empty, because you've set so in previous run) it reassigns `$lastviewedarticles` empty array). :) – Leri Mar 08 '13 at 10:42
  • not sure if i understand what you mean... could you edit the part of the code that isn't working ? – 456543646346 Mar 08 '13 at 10:55
0

As of RFC 2109, the domain parameter has to be set with a starting dot (see http://php.net/setcookie). Please try that, your code seems to be okay. And please remove the single-quotes from '.$domain.'.

webmonkey
  • 1,083
  • 1
  • 15
  • 33