Hello, help please? I wish to develop a module to do something very simple. I am challenged by the Drupal API.

Goal:

1) Determine if user is viewing a particular node (role is irrelevant)
2) If yes, check to see if cookie is set

  • If cookie is set, do nothing
  • If cookie is not set, then set cookie and then redirect user to another node

    That's it!

    I have created a module and installed it, there is no error yet it also does nothing. No cookie is set. I am not sure how the Drupal system likes to redirect requests so insight there would be helpful, please.

    <?php
    //$Id: offer_survey.module,v 1.0 2009/09/21 11:31:55 Stoob Exp $
    function offer_survey_init() {
      global $base_url;
    
      $offer_survey = true;
      $cookie_name = 'survey_offered';
      
      if ($node->nid == 651) {
        if ($_COOKIE[$cookie_name]) {
          // do nothing
        } else {
          setcookie($cookie_name,1,time() + (86400 * 365));
          //then do the redirect an internal webform URL
        }
      } 
    }
    
  • Comments

    briansea’s picture

    You reference $nod->nid but $node does not exist in the local function scope. You should be able to use arg(1). So if (arg(1) == 651)

    stoob’s picture

    contents of modulename.module file:

    <?php
    //$Id: offer_survey.module,v 1.0 2009/09/21 11:31:55 Stoob Exp $
    function offer_survey_init() {
      global $base_url;
      global $cookie_domain;
      $cookie_name = 'survey_offered';  //each site must have a unique name
      
      if (arg(1) == 2) {   // the number of the node (nid) of the page
      	if (!isset($_COOKIE[$cookie_name])) {
    	  setcookie($cookie_name,1,time() + (86400 * 365),null,$cookie_domain); //lasts a year
    	  drupal_goto('new/destination'); // the path to be redirected to
    	} 
      } 
    }
    
    
    briansea’s picture

    good!

    suffering drupal’s picture

    Could this code, in some adapted way, be used to set a cookie for the author of the landing page (all user roles) and later be retrieved through view to use it in an external URL? The intention is to know which user-input page generated the visit in case of sales and use his/her reference to for sharing revenue.

    In case of answer you should start to explain from the beginning, I don't know how to set cookie in the first place, nor how or where to put the corresponding code (php-block?).

    I started with Drupal in 2007 and then my life got stuck...

    stoob’s picture

    setcookie is easy, using the setcookie() php function

    I do not understand what your goal is. I am sorry but I am too busy to help you, and this project was almost two years ago, I don't remember much about it. Good luck.