Hello,
I have a problem I can not solve: I'm unable to read a cookie value in my theme.
I have a form that allows you to choose a country from a list.
It sends it to a PHP page that generates a cookie whose value is the country code, like this:

<?php
setcookie ('cookiePays', $_POST['selectPays'], time () + 432000);
$cookiePaysChoisi = $_COOKIE['cookiePays'];
?>

Then we are redirected to the site home page.
Also I have a module that is supposed to read the contents of the cookie and pass it in a global variable, like this:

<?php 
global $cookPays;
if (isset ($_COOKIE['cookiePays'])) {
   $cookPays = $_COOKIE['cookiePays'];
}
else {
$cookPays = "xx";
}
?>

Finally, in page.tpl.php, I try to show that famous country code:

<h3 style="position: absolute; background: white;"> SELECTED COUNTRIES: <?php global $cookPays; echo $cookPays ?></ h3> 

Well, I ALWAYS get the result: SELECT COUNTRY: XX!
The cookie is never read.
While in the php page that reads the form, I send me the result of the cookie by email, to make sure it is correct, that is!
Note that I have tried with a session variable, and I get the same result.
I also tried to read the result in html.tpl.php, in a function in template.php, always the same!
Whenever I'm in Drupal, I can not read my cookie. I searched a long time on the Internet I found nothing there something I'm missing!

Does anyone have a solution or idea? Is there something special I have to to to read cookies or session variable, or is the problem only in my Drupal installation ?

Thank you in advance!

Comments

meybeck’s picture

Actually, i can read the $_COOKIE variable, but my cookie is not inside.
When I create it, in another page, I can access to its value if I'm not in my Drupal site.
But when I visit my Drupal website homepage, my cookie is destroyed ! It's not in the $_COOKIE variable anymore !
Has somebody got an idea ? Would be great !

meybeck’s picture

OK I just forgot the path parameter to make the cookie available in all directories. That's all folks.

adityaj’s picture

I have the same problem here is my code snippet, if any one can tell where i am going wrong

   setcookie('front_page','yes', time() + (86400 * 365), "/");
   echo $_COOKIE['front_page'] .'cookie';

Though in chrome browser i can cookie being set, but unable to read in page.tpl.php or template.php (hook_preprocess_page).

Any Insight

adityaj’s picture

I have the same problem here is my code snippet, if any one can tell where i am going wrong

   setcookie('front_page','yes', time() + (86400 * 365), "/");
   echo $_COOKIE['front_page'] .'cookie';

Though in chrome browser i can cookie being set, but unable to read in page.tpl.php or template.php (hook_preprocess_page).

Any Insight

arpas’s picture

setcookie('cookie_name', $value, $valid_to_timestamp, '/');
// Set cookie value to access $_COOKIE immediately.
$_COOKIE['cookie_name'] = $value;

Probably last line must help.

bdlangton’s picture

$_COOKIE['cookie_name'] = $value;

This is just the same as assigning it to any other variable. It'll be available in the same script, but it's not actually saving the value to a cookie. It will no longer be in the $_COOKIE array after another page load.