0

I just copy and pasted set cookie sample code(http://www.w3schools.com/php/php_cookies.asp) to my module and tpl.php

In my module, I put the setcookie in my submit function as below:

function custom_request_price_form_submit($form,&$form_state){
   $cookie_name = "user";
   $cookie_value = "John Doe";
   setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

   if(!isset($_COOKIE[$cookie_name])) {
       drupal_set_message("Cookie named '" . $cookie_name . "' is not set!");
   } else {
       drupal_set_message("Cookie '" . $cookie_name . "' is set!<br>");
       drupal_set_message("Value is: " . $_COOKIE[$cookie_name]);
   }
}

Im my node--XXX.tpl.php, I just copy the code without modification as below:

$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

However, no matter how I refresh the webpage, the message always shows "Cookie named 'user' is not set!"

I also tried user_cookie_save function, but no luck. Does anyone know why my cookie setting doesn't work?

1 Answer 1

0

Thank you everyone, I got the answer. It seems Drupal supports session instead of cookie.(Sorry I cannot find the reference again.) Therefore, I used session to implement my function.

Here are some reference:

Example for session: https://www.drupal.org/node/360542

How to set session expiration: https://www.drupalgardens.com/documentation/cookie/expire

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.